entity framework复合键不允许重复数据

entity framework composite key not allowing duplicate data

我首先使用 entity framework 代码设计一个 SQL 数据库,然后使用 "SEED" 方法用初始数据填充数据库。以下是具有一对多关系的两个模型。 "Foo"可以有多个"FooSection"

public class Foo {
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int FooId {get; set;}
   // Some more properties.
   // Navigation collection
   public virtual ICollection<FooSection> fooSections {get; set;}
}

public class FooSection {
   // Has composite key
   [ForeignKey("foo"), Column(Order=1)]
   public int FooId {get; set;}       
   [Key, Column(Order=2)]
   public string SectionName {get; set;}
   // Some more properties
   // Navigation property
   public virtual Foo foo {get; set;}
}

假设 FooId = 1 的 "Foo" 的第一个实例有 2 个 "FooSection",而 FooId = 2 的 "Foo" 的第二个实例有 1 个 "FooSection"。所以我的种子方法看起来像这样 -

 protected override void Seed(PDI.Cloud.Models.ApplicationDbContext context)
 {
    // First add data for "Foo"
    var FooList = new List<Foo>();
    if(!(context.Foos.Any()))
    {
        // First instance of Foo
        FooList.Add( 
           new Foo{
             // Assign values to properties here
           }
        );
        // Second instance of Foo
        FooList.Add( 
           new Foo{
             // Assign values to properties here
           }
        );
       FooList.ForEach(f => context.Foos.AddOrUpdate(f));
       context.SaveChanges();

       // Get info of "FooSection"
       var fooSectList = getFooSectList(context); 
       // Assign "FooSection"s to respective "Foo".
       FooList[0].fooSections.Add(fooSectList[0]); // Section 1 for Foo with id = 1
       FooList[0].fooSections.Add(fooSectList[1]); // Section 2 for Foo with id = 1
       FooList[1].fooSections.Add(fooSectList[2]); // Section 1 for Foo with id = 2
       Context.SaveChanges();
    }
 }

private List<FooSection>getFooSectList(PDI.Cloud.Models.ApplicationDbContext context)
    {
       var FooSectList = new List<FooSection>();
       if(!(context.FooSections.Any()))
       {
        // 1st FooSection for Foo with FooId = 1
        FooSectList.Add( 
           new FooSection{
             FooId = 1,
             SectionName = "Sect1"
           }
        );
        // 2nd FooSection for Foo with FooId = 1
        FooSectList.Add( 
           new FooSection{
             FooId = 1,
             SectionName = "Sect2"
           }
        );
        // 1st FooSection for Foo with FooId = 2
        FooSectList.Add( 
           new FooSection{
             FooId = 2,
             SectionName = "Sect1"
           }
        );
       FooSectList.ForEach(f => context.FooSections.AddOrUpdate(f));
       context.SaveChanges();
      }
      return FooSectList;
    }

当我尝试 运行 种子方法时,它给了我 SQLException "Violation of PRIMARY KEY constraint . Cannot insert duplicate key in object. The duplicate key value is (0, Sect1)"

我是不是漏掉了什么?因为我认为对于复合键,只要组合是唯一的,我就不应该得到这样的错误。

如有任何帮助,我将不胜感激。

谢谢。

我认为您希望 FooSection 具有 FooId, SectionName 的复合主键。但是,按照您现在的方式,您的主键只有 SectionName

您必须将 Key 属性添加到 FooId 属性 以将其变成复合主键:

[Key, ForeignKey("foo"), Column(Order=1)]
public int FooId {get; set;}