EF4 Code First CTP5:种子方法不再有效

发布于 2024-10-07 06:10:21 字数 1455 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-10-14 06:10:21

也许是因为您使用的变量名已由 EF 生成为复数形式。

而不是:

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var Members = new List<Member>
        {
            new Member {
                Email = "[email protected]",
                City = "San Francisco",
.....

尝试这个

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var _members = new List<Member>
        {
            new Member {
                Email = "[email protected]",
                City = "San Francisco",

....


_members.ForEach(m => context.Members.Add(m));
        context.SaveChanges();

我将你的代码与 Scott Gu 的进行比较
http:// /weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

Maybe because your variable name that you using has been generated as pluralized by EF.

Instead of :

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var Members = new List<Member>
        {
            new Member {
                Email = "[email protected]",
                City = "San Francisco",
.....

try this

public class Initializer : DropCreateDatabaseAlways<MeetPplDB>
{
    protected override void Seed(MeetPplDB context)
    {
        var _members = new List<Member>
        {
            new Member {
                Email = "[email protected]",
                City = "San Francisco",

....


_members.ForEach(m => context.Members.Add(m));
        context.SaveChanges();

I compare your code with Scott Gu's
http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

关于从前 2024-10-14 06:10:21

异常的原因是您将数据类型 int 与 DatabaseGenerationOption.Identity 结合在一起。使用 Identity 将生成 guid 而不是 int key。

我有一个类似的问题,因为我想要一个 int 类型的 Key 和一个 Guid 作为唯一的 rowid。如果我在一个实体上使用两者,即使它们位于单独的道具中,种子也会停止工作。

The reason for your exception is that you have datatype int combined with DatabaseGenerationOption.Identity. Using identity will generate a guid and not a int key.

I have a similar problem because I want to have a Key of type int and a Guid as a unique rowid. And seed stops working if I use both on a entity even if they're in separate props.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文