Ginko Ginko - 21 days ago 9
ASP.NET (C#) Question

Issue with initializing database with EF

I'm trying to initialize a database with sample data and I get an error:


Failed to set database initializer of type
'Blog.Models.DAL.BlogInitializer,Blog' for DbContext type
'Blog.Models.DAL.BlogContext, Blog' specified in the application


Inner exception is like that:


Can't find a file or an assembly 'Blog' or one of its dependencies.
Can't find the specified file.


I have Blog.Model and Blog.WebUI in separate projects in the same Blog solution.
I have edited Web.config (In Blog.WebUI) file like that:

<connectionStrings>
<add name="BlogContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=Blog;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>

<entityFramework>
<contexts>
<context type="Blog.Models.DAL.BlogContext, Blog">
<databaseInitializer type="Blog.Models.DAL.BlogInitializer, Blog" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>


In Server Explorer I see an accessible database created by it, but no tables inside.

BlogContext class:

namespace Blog.Models.DAL
{
public class BlogContext : DbContext
{

public BlogContext() : base("BlogContext")
{
}

public DbSet<Post> Posts { get; set; }

}
}


And initializer:

namespace Blog.Models.DAL
{
public class BlogInitializer : DropCreateDatabaseIfModelChanges<BlogContext>
{
protected override void Seed(BlogContext context)
{
var posts = new List<Post>
{
new Post { Title="sadasd", Content="asdsa", Date=DateTime.Parse("2016-11-13"), Language="pl" },
new Post { Title="sadasd", Content="asdsa", Date=DateTime.Parse("2016-11-13"), Language="pl" },
new Post { Title="sadasd", Content="asdsa", Date=DateTime.Parse("2016-11-13"), Language="pl" },
new Post { Title="sadasd", Content="asdsa", Date=DateTime.Parse("2016-11-13"), Language="pl" },
new Post { Title="sadasd", Content="asdsa", Date=DateTime.Parse("2016-11-13"), Language="pl" }
};

posts.ForEach(p => context.Posts.Add(p));
context.SaveChanges();
}
}
}

Answer

EF failed to instantiate the database initalizer for your DbContext instance named BlogContext.

In the configuration you have configured the correct value for type of your database initializer which is correct:

<contexts>
  <context type="Blog.Models.DAL.BlogContext, Blog">
    <databaseInitializer type="Blog.Models.DAL.BlogInitializer, Blog" />
  </context>
</contexts>

The issue is that you're not using the correct assembly name Blog. You need to go to your project "Properties" via the context menu or Alt + F4 and copy the correct assembly name. Then put it like this:

<contexts>
  <context type="Blog.Models.DAL.BlogContext, [The correct assemblby name]">
    <databaseInitializer type="Blog.Models.DAL.BlogInitializer, [The correct assemblby name]" />
  </context>
</contexts>
Comments