简体   繁体   中英

can't generate DB from model code in entity framework

I am learning EF.I have some classes of model and class that inherited from DbContext. I have intializer Initializer : DropCreateDatabaseIfModelChanges with empty method Seed.I call

 Database.SetInitializer<...Context>(new ...Initializer());

And I have set new add to connection strings

<add name="...Context" connectionString="Data Source=|DataDirectory|Temp.sdf" providerName="System.Data.SqlServerCe.4.0"/>

but I can't generate DB from this.App Data is empty. Please help me!Probably I forgot something?My method Seed I have left is empty but I don't think it's a trouble Thank u!

Setting a initializer is not enough. You need at least start a query.

In the moment you try to get data from your context, the process will start and if your database does not exists the will get created.

MyDbContext myDbContext = new MyDbContext();
var firstItemOfSomething = myDbContext.FirstItemOfSomething.FirstOrDefault(); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM