EF Code First中自引用外键的语法是什么? [英] What is the syntax for self referencing foreign keys in EF Code First?

查看:199
本文介绍了EF Code First中自引用外键的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在联系人表中引用SpouseId的外键到Id。这样做的语法是什么?我似乎找不到一个例子。谢谢。

I am trying to reference a foreign key from SpouseId to Id in the Contact table. What is the syntax for doing this? I can't seem to find an example. Thanks.

我有一个这样的类:

public class Contact
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int? SpouseId {get;set;}
}

EDIT1
根据链接由Joel Cunningham提供,而Morteza的答案我已经添加了一些附加代码。

EDIT1 Per the link provided by Joel Cunningham and the answer from Morteza I have added some additional code.

ContactMap.cs

ContactMap.cs

public partial class ContactMap : EntityTypeConfiguration<Contact>
{
  public ContactMap()
     {
       this.ToTable("Contact");
       this.HasKey(c => c.Id);
       this.HasOptional(c => c.Spouse)
           .WithMany()
           .IsIndependent()
           .Map(m => m.MapKey(fk => fk.Id, "SpouseId"));
     }
}

MyObjectContext.cs

MyObjectContext.cs

public class MyObjectContext : DbContext, IDbContext
{
  public DbSet<Contact> Contacts {get;set;}
  protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Configurations.Add(new ContactMap());
     }
}

注意:我还添加了[ForeignKey SpouseId)]属性到我的联系人类中的我的配偶属性。不幸的是,我不断得到序列包含多个匹配的元素。

Note: I also added the "[ForeignKey("SpouseId")]" attribute to my Spouse property in my Contact class. Unfortunately I keep getting "Sequence contains more than one matching element".

EDIT2:
Morteza的答案是正确的。总结:对于自引用外键,您可以将该属性标记为[ForeginKey(SpouseId)]或使用以下流畅的API示例。我在某些评论中报告的错误是由我的单元测试引起的生成db的正确方法。我发现一个很好的链接其中 Craig Stuntz 概述了为什么自动递增键和自引用外键会导致无法确定依赖操作的有效排序 错误,我相信这是我的问题,希望这有助于某人。

Morteza's answers below is correct. To summarize: For self referencing foreign keys you can either mark the property as a "[ForeginKey("SpouseId")] OR use the fluent API example below. The errors I reported in some of my comments were caused by my unit test. EF generated the db the correct way. I found a good link where Craig Stuntz outlined why auto-increment keys and self-referencing foreign keys can cause the "Unable to determine a valid ordering for dependent operations" error. I believe this is what my problem is. Hope this helps someone.

推荐答案

p>

Something like this will work:

public class Contact
{
    public int Id {get;set;}
    public string Name {get;set;}
    public int? SpouseId {get;set;}

    [ForeignKey("SpouseId")]
    public Contact Spouse {get;set;}
}

ForeignKeyAttribute 已被CTP5程序集添加到 System.ComponentModel.DataAnnotations

ForeignKeyAttribute has been added to System.ComponentModel.DataAnnotations by CTP5 assembly.

由于CTP5中的错误,创建独立自我引用关联会引发异常。解决方法是使用外键关联(无论如何都是建议的)

Due to a bug in CTP5, creating an Independent Self Referencing Associations throws an exception. The workaround is to use Foreign Key Associations instead (which is always recommended regardless).

如果您愿意,还可以使用流畅的API来实现此目的:

You can also use fluent API to achieve this, if you prefer:

public class Contact
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? SpouseId { get; set; }                

    public Contact Spouse { get; set; }
}

public class Ctp5Context : DbContext
{
    public DbSet<Contact> Contacts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Contact>()
                    .HasOptional(c => c.Spouse)
                    .WithMany()
                    .HasForeignKey(c => c.SpouseId);
    }
}

使用模型:

using (var context = new Ctp5Context())
{
    Contact contact = new Contact()
    {
        Name = "Morteza",
        Spouse = new Contact()
        {
            Name = "Code-First"
        }
    };
    context.Contacts.Add(contact);
    context.SaveChanges();
}

这篇关于EF Code First中自引用外键的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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