简体   繁体   English

无法从实体框架中的模型代码生成DB

[英]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.

相关问题 如何从Entity Framework 4.3代码优先模型生成DDL脚本? - How can I generate DDL scripts from Entity Framework 4.3 Code-First Model? 如何基于Entity Framework代码优先模型自动生成.mdf db(如果不存在) - How to automatically generate .mdf db if it does not exist based on Entity Framework code-first model 实体框架无法从数据库正确创建模型 - Entity Framework can't properly create model from database 实体框架无法生成数据库 - Entity Framework can't generate database 使用模型优先生成代码时出现实体框架错误 - Entity Framework error when using Model First to generate code 无法同步实体框架模型和数据库 - Can't sync Entity Framework model and database 实体框架DB-First - 我可以从基础模型继承以添加我的功能吗? - Entity Framework DB-First - can I inherit from base model in order to add my functionality? Entity Framework 4 从具有多个模式的模型生成数据库 - Entity Framework 4 Generate Database From Model With Multiple Schemas 从Visual Studio数据库项目生成实体框架模型 - Generate Entity Framework model from Visual Studio database project ADO.Net Entity Framework从XSD文件生成模型 - ADO.Net Entity Framework generate model from XSD file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM