EF6 CodeFirst My [Key] Id列不会自动递增,因为标识列应该是 [英] EF6 CodeFirst My [Key] Id Column is not auto-incrementing as an identity column should

查看:389
本文介绍了EF6 CodeFirst My [Key] Id列不会自动递增,因为标识列应该是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个类,我需要从一个持有Id的普通基类派生。忽略其中的一个,我们假设我们有:

  public class MyBase {
[Key ]
public int Id {get;组; }
}
public class MyName:MyBase {
public string Name {get;组; }
}

我的上下文(DataContext)如下所示:

  public DbSet&MyName> MyNames {get;组; } 

//避免让EF创建一个MyBases表,而是将
// MyBase.Id映射到MyNames和我的其他派生类中,这样做...

protected override void OnModelCreating((DbModelBuilder modelBuilder){
modelBuilder.Entity< MyBase>()
.Property(c => c.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None) ;

modelBuilder.Entity< MyName>()
.Map(m => {
m.MapInheritedProperties();
m.ToTable(MyNames );
});
}

结果DB,当我启用-Migrations,Add-Migration Initial和Update-Database是我没有名为MyBases的表,而是在MyNames中获取Id列。

  dbo.MyNames 
-----------
Id(PK,int,not null)
名称(nvarchar(max),null)

到目前为止这么好,所有的c ompiles和builds,然后我使用如下所示的测试:

  using(DataContext dc = new DataContext()){ 
var jj = new MyName {Name =Janice Joplin};
dc.MyNames.Add(jj);
dc.SaveChanges();

var jh = new MyName {Name =Jimmy Hendrix};
dc.MyNames.Add(jh);
dc.SaveChanges();
}

这是第一次(Janice添加了Id = 0)但 not 第二个... Jimmy得到一个DUPLICATE KEY异常。注意(完全公开)我实际上在我的代码的另一部分中创建了jj和jh对象,然后将它们传递到这个方法(上面)作为MyBase对象,然后将它们重命名为MyName对象,如果是这样。我希望这不是一个问题。



我想如果一切都在一张表中,则可以将该标识标识为身份,并且@@ IDENTITY可用于分配对象Id值。也许我将需要创建一个MyBases表,并先创建该记录,然后将该Id重复到事务中的派生表中。这是最好的方法?



对于这款EF6 CodeFirst新手来说,任何帮助都不胜感激。谢谢。

解决方案

我记得当我曾经做过EF时,我将创建一个带有Identity的表,在类中我将属性id列与

  [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

所以我假设你的代码应该是

  modelBuilder.Entity< MyBase> ;()
.Property(c => c.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);


I have several classes that I need to derive from a common base class which holds an Id. Ignoring the all but one of these for the moment, let's say we have:

public class MyBase {
   [Key]
   public int Id { get; set; } 
}
public class MyName : MyBase {
   public string Name { get; set; }
}

my context (DataContext) looks like this:

public DbSet<MyName>MyNames { get; set; }

// to avoid having EF make a MyBases table and instead map 
// MyBase.Id into MyNames and my other derived classes I do this ...

protected override void OnModelCreating((DbModelBuilder modelBuilder) {
   modelBuilder.Entity<MyBase>()
            .Property(c => c.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

   modelBuilder.Entity<MyName>()
            .Map(m => {
                    m.MapInheritedProperties();
                    m.ToTable("MyNames");
                });
}

The resulting DB, when I Enable-Migrations, Add-Migration Initial, and Update-Database is that I get no table called MyBases, but instead I get the Id column in MyNames

   dbo.MyNames
   -----------
   Id (PK, int, not null)
   Name (nvarchar(max), null)

So far so good, all of that compiles and builds, and then I test it using something like the following:

   using ( DataContext dc = new DataContext()) {
      var jj = new MyName { Name = "Janice Joplin" };
      dc.MyNames.Add(jj);
      dc.SaveChanges();

      var jh = new MyName { Name = "Jimmy Hendrix" };
      dc.MyNames.Add(jh);
      dc.SaveChanges();
   }

This works the first time (Janice is added with Id = 0) but not the second ... Jimmy gets a DUPLICATE KEY exception. Note (for full disclosure) I'm actually creating the jj and jh objects in another part of my code, then passing them into this method (above) as MyBase objects, then casting them back to MyName objects if that's what they are. I hope that isn't a problem.

I guess if everything were in one table, the Id could be marked as Identity and @@IDENTITY could be used to assign the object Id value. Perhaps I will need to make a MyBases table after all, and create that record first, then duplicate the Id into the derived tables in a transaction. What's the best approach for this?

Any help for this EF6 CodeFirst newbie would be appreciated. THANKS.

解决方案

I remember when I used to do EF I would create a table with Identity and in the class I would attribute the id column with

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

so I am assuming that your code should be

   modelBuilder.Entity<MyBase>()
        .Property(c => c.Id)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

这篇关于EF6 CodeFirst My [Key] Id列不会自动递增,因为标识列应该是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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