[英]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. 我正在学习EF。我有一些继承自DbContext的模型和类。 I have intializer Initializer : DropCreateDatabaseIfModelChanges
with empty method Seed.I call 我有初始化器Initializer : DropCreateDatabaseIfModelChanges
与空方法Seed.I调用
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. 但我无法从此生成数据库.App数据为空。 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.