简体   繁体   English

如何通过覆盖Seed方法将数据库与EF模型同步?

[英]How to synchronize database with an EF model by overriding the Seed method?

I'm new to EF Code First approach and got a question. 我是EF Code First方法的新手,但遇到了一个问题。 I've created a sample SQL database using the mentioned approach. 我使用上述方法创建了一个示例SQL数据库。 It works as expected. 它按预期工作。 My problem is how to update my tables as the model changes? 我的问题是随着模型的更改如何更新表? I've read and surf the web for that and finally the following solutions figured out: 1. DropCreateDatabaseIfModelChanges 2. DropCreateDatabaseAlways 3. CreateDatabaseIfNotExists 4. Using Enable-Migrations in PM console that unfortunately coped with lots of syntax errors! 我已经阅读并在网上冲浪,最终找到了以下解决方案:1. DropCreateDatabaseIfModelChanges 2. DropCreateDatabaseAlways 3. CreateDatabaseIfNotExists 4.在PM控制台中使用Enable-Migrations不幸地解决了许多语法错误! I know I need to override the Seed method which could be inherited from DropCreateDatabaseAlways. 我知道我需要重写可以从DropCreateDatabaseAlways继承的Seed方法。 And I know I have to create objects in this method and add them to the context. 而且我知道我必须使用此方法创建对象并将其添加到上下文中。 BUT... what if my Customer table includes 100 records? 但是...如果我的客户表包含100条记录怎么办? Do I have to fetch them and add them to the context using a loop? 我是否必须获取它们并使用循环将它们添加到上下文中? I don't know but it seems not reasonable to me! 我不知道,但对我来说似乎不合理!

I appreciate any one could give a good solution. 我感谢任何人都可以提供一个好的解决方案。 Thanks you 谢谢

I have to say I personally think the easiest method is to use Migrations. 我不得不说我个人认为最简单的方法是使用迁移。 I use the method mentioned here, under the heading "Your First Automatic Migration": 我使用此处提到的方法,在“您的第一次自动迁移”标题下:

http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx

(I use EF 5.0 even if the article says 4.3, there is noe difference) (即使文章说4.3,我也使用EF 5.0,没有区别)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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