简体   繁体   English

实体框架外键映射

[英]Entity Framework Foreign Key Mapping

I'm trying to determine what EF6 is telling me, and it's not making much sense to me, so I'm hoping someone here can clarify this. 我正在试图确定EF6告诉我什么,这对我来说没有多大意义,所以我希望有人在这里澄清一下。

I'm setting up my FluentApi as so (composite keys in use in the DB, this is Code First from Database): 我正在设置我的FluentApi(在DB中使用的复合键,这是来自数据库的Code First):

modelBuilder.Entity<Object1>()
    .HasKey(e => new { e.Property1, e.Property2 }
    .HasMany(e => e.Object2s)
    .WithRequired(e => e.Object1)
    .HasForeignKey(e => new { e.Property1, e.Property2 });

modelBuilder.Entity<Object2>()
    .HasKey(e => new { e.Property2, e.Property3, e.Property1 })
    .HasRequired(e => e.Object1)
    .WithMany(e => e.Object2s)
    .HasForeignKey(e => new { e.Property1, e.Property2 });

All builds fine, but when I go to select anything, I get this: 所有构建都很好,但当我去选择任何东西时,我得到这个:

"Foreign key constraint 'Object1_Object2' from table Object2 (Property2, Property1) to table Object1 (Property1, Property2):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side." “从表Object2(Property2,Property1)到表Object1(Property1,Property2)的外键约束'Object1_Object2'::映射不足:外键必须映射到参与概念方的外键关联的某些AssociationSet或EntitySets。”

Ideas? 想法? I don't understand why the FK constraint is showing Object2's FK in the incorrect order, when I've defined the correct order in the FluentApi. 当我在FluentApi中定义了正确的顺序时,我不明白为什么FK约束以错误的顺序显示Object2的FK。

Try attribute : 尝试属性:

//using System.ComponentModel.DataAnnotations.Schema

public class Object1
{

    public int Property1{ get; set; }
    public string Property2 { get; set; }

    //Foreign key for Object2
    public int FK_Object2_Property { get; set; }

    [ForeignKey("FK_Object2_Property")]
    public Object2 Object2 { get; set; }
}
public class Object2
{

    public int Property1{ get; set; }
    public string Property2 { get; set; }

    public ICollection<Object1> Objects { get; set; }
}

This is ultimately what I had to do - set the Foreign Key attribute, and specify Column order. 这最终是我必须做的 - 设置外键属性,并指定列顺序。 The Code First from DB had Order=2 and Order=0 reversed, so I changed it to: DB中的Code First具有Order = 2和Order = 0,因此我将其更改为:

public partial class Object2
{
    [Key]
    [Column("Property2", Order=2)]
    [ForeignKey("Object1")]
    public short Property2 { get; set; }

    [Key]
    [Column("Property3", Order=1)]
    public short Property3 { get; set; }

    [Key]
    [Column("Property1", Order=0)]
    [ForeignKey("Object1")]
    public int Property1 { get; set; }
}

I removed the FluentApi from both Object1 and Object2 relating to this foreign key arrangement, and all appears well. 我从Object1和Object2中删除了与此外键排列有关的FluentApi,并且一切都很好。 I'll re-do it in FluentApi as soon as I determine how to set the column order in FluentApi. 一旦我确定如何在FluentApi中设置列顺序,我就会在FluentApi中重新执行此操作。

Then I have to add this to my T4 file for this object... ultimate goal is to be able to regenerate the DB to Code First at any time without issue :) Thanks for the help, all! 然后我必须将此添加到我的T4文件中以获取此对象...最终目标是能够在任何时候将DB重新生成到Code First而没有问题:)感谢所有人的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM