如何使用EntityFramework进行种子数据首先进行迁移 [英] How to seed data using EntityFramework Code first Migrations

查看:77
本文介绍了如何使用EntityFramework进行种子数据首先进行迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个nuGet软件包的Beta 1版本,数据库已经被创建了,我需要知道是否有办法通过迁移来填充我的表。 Thanxs

解决方案

介绍文章显示如何种子数据
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆