实体框架组合键不允许重复数据

时间:2016-07-12 16:26:47

标签: entity-framework composite-key

我首先使用实体​​框架代码来设计SQL数据库和" SEED"用初始数据填充数据库的方法。以下是具有一对多关系的两个模型。 "富"可以有很多" 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;}
}

说第一个&#34; Foo&#34; FooId = 1,有2&#34; FooSection&#34; &#34; Foo&#34;的第二个实例FooId = 2,有1&#34; FooSection&#34;。所以我的种子方法看起来像这样 -

 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&#34;违反PRIMARY KEY约束。无法在对象中插入重复键。重复键值为(0,Sect1)&#34;

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

我将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

我认为您期望FooSection拥有FooId, SectionName的复合主键。但是,您现在的方式是,主键仅为SectionName

您必须将Key属性添加到FooId属性才能将其转换为复合主键:

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