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

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

问题描述

你好我使用Beta 1的版本的NuGet包,被媒体链接创建了数据库,我需要知道,如果没有通过迁移来填充我的表的方式。 Thanxs


解决方案

的介绍后展示了如何种子数据
<一href=\"http://blogs.msdn.com/b/adonet/archive/2011/11/29/$c$c-first-migrations-beta-1-no-magic-walkthrough.aspx\">http://blogs.msdn.com/b/adonet/archive/2011/11/29/$c$c-first-migrations-beta-1-no-magic-walkthrough.aspx


  

种子数据:
  覆盖在这个类种子的方法添加种子数据。
   - 种子法将迁移到最新版本后调用。
   - 您可以使用DbContext.AddOrUpdate()辅助扩展方法,以避免产生
  重复的种子数据。例如。


  myContext.AddOrUpdate(C =&GT; c.FullName,
  新的客户{=全名彼得斯安德鲁,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 code首先迁移到种子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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