I have simple self-host Web API application. I installed EnityFramework6 package and added following lines in App.config under <enityframework> section:
<contexts>
<context type="simple_api.MyContext, simple_api">
<databaseInitializer type="simple_api.MyInitializer, simple_api" />
</context>
</contexts>
Initializer class is like following:
public class MyInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<Context>
{
protected override void Seed(Context context)
{
Console.log("My seed method");
var persons = new List<Person> { };
var john = new Person() { Firstname = "John", Lastname = "Doe" };
context.Persons.Add(john);
context.SaveChanges();
}
}
I enabled and created migration, but the problem is that running it does not trigger my Seed method:
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201805210804206_initial].
Applying explicit migration: 201805210804206_initial.
Running Seed method.
I also tried changing App.config , but It seems to be totally ignored, because <context type="foobar, nomatterwhatishere">
does not trigger any warning nor error.
What can be the problem?
--
By the way, when I configured log4net , file was ignored also and I have to call log4net.Config.XmlConfigurator.Configure()
. Maybe there is similar thing for EntityFramework?
I was wrong: config file works, but Seed method for DropCreateDatabaseIfModelChanges is not triggered. I replaced it with DropCreateDatabaseAlways and after querying model it throwed exception:
Failed to set database initializer of type 'simple_api.MyInitializer, simple_api' for DbContext type 'simple_api.MyContext, simple_api' specified in the application configuration. See inner exception for details.
After some debugging I figured out that namespace is simple_api
, but assembly name is simple-api
, so configuration should be as follows:
...
<entityframework>
<contexts>
<context type="simple_api.MyContext, simple-api">
<databaseInitializer type="simple_api.MyInitializer, simple-api" />
</context>
</contexts>
...
</entityframework>
...
Now everything is working, but I am not sure why Seed was not called for DropCreateDatabaseIfModelChanges .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.