如何使用EntityFramework进行种子数据首先进行迁移 [英] How to seed data using EntityFramework Code first Migrations
问题描述
介绍文章显示如何种子数据
http://blogs.msdn.com/b /adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx
种子数据:
覆盖此类中的Seed方法以添加种子数据。
- 迁移到最新版本后,将会调用Seed方法。
- 您可以使用DbContext.AddOrUpdate()辅助扩展方法来避免创建
重复的种子数据。例如
myContext.AddOrUpdate(c => c.FullName,
new客户{FullName =Andrew Peters,CustomerNumber = 123},
);
Hi I'm using the Beta 1 version of this nuGet package, the database is allready created and I need to know if there is a way to populate my tables through migrations. Thanxs
The intro post shows how to seed data http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx
Seed data: Override the Seed method in this class to add seed data. - The Seed method will be called after migrating to the latest version. - You can use the DbContext.AddOrUpdate() helper extension method to avoid creating duplicate seed data. E.g.
myContext.AddOrUpdate(c => c.FullName,
new Customer { FullName = "Andrew Peters", CustomerNumber = 123 },
);
这篇关于如何使用EntityFramework进行种子数据首先进行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!