0

I have an ASP.NET MVC solution with EntityFramework. The DbContext and data model are in different projects.

DataModel project:

namespace DataModel
{
    public class Function
    {
        public int FunId { get; set; }
        public string Name { get; set; }
     }
}

DataAccess project: with using System.Data.Entity; using DataModel;

namespace DataAccess
{
    public class DbData : DbContext
    {
        public DbData()
               : base("name=dbconn")
        {
        }

        public DbSet<Function> functions { get; set; }

    }
}

DataAccess app.config connection string:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" >
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="dbconn" providerName="System.Data.SqlClient" connectionString="Server=MYPC\SQL2012;MultipleActiveResultSets=True;Database=DBDemo;Persist Security Info=True;Integrated Security=SSPI;" />
  </connectionStrings>
</configuration>

Web project references other two projects. The index page has the following code:

public ActionResult Index()
    {
        try
        {
            DbData ctx = new DbData();
            ctx.SaveChanges();
        }
        catch (Exception e)
        {
        }
        return View();
    }

Web project wbe.config connection string

  <connectionStrings>
<add name="dbconn" providerName="System.Data.SqlClient" connectionString="Server=MYPC\SQL2012;MultipleActiveResultSets=True;Database=DBDemo;Persist Security Info=True;Integrated Security=SSPI;" />
  </connectionStrings>

but after running project i cannot see the database. If i check the DbContext instance ( ctx object) i see these errors:

Invalid operation. The connection is closed.

enter image description here

If i put DbContext class inside web project everything is ok.

mrBlack
  • 1
  • 4
  • What *changes* are you trying to save in that code? You didn't modify anything, you just created an instance of a class. What is that `DbData` class? Is that your EF context? – David Oct 25 '17 at 14:07
  • Yes, DbData class extends DbContext. This is the first operation, database doesn't exist yet. I just create 3 projects. One with model class, one with DbContext and another one is the web project. – mrBlack Oct 25 '17 at 14:10
  • It's hard to determine an answer without more code. If `DbContext` is working and `DbData` which you derive from it isn't. Something is probably wrong in the implementation of that. – Joshua Morgan Oct 25 '17 at 14:21
  • need something more? I don't understand what this few code lines don't work. If i put everything into web project it works. How i should use model and data access layers in different project? – mrBlack Oct 25 '17 at 15:26
  • As David said, the code doesn't make much sense. You are not saving anything. Regardless, are you getting an exception? At what line? Also, you usually want your context inside a `using` statement or better yet IoC container. See [here](https://msdn.microsoft.com/en-us/library/jj729737(v=vs.113).aspx). – Steve Greene Oct 25 '17 at 15:33

1 Answers1

0

Thank u guys, at the end I added an object of Function class to the "ctx" instance, and it worked. Now i can see that the database exists. So the question now is, how i could create an empty database with empty tables only?

mrBlack
  • 1
  • 4