代码优先循环引用外键配置 [英] Code First Circular Reference Foreign Key Configuration

查看:148
本文介绍了代码优先循环引用外键配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Parent 
{
public int Id {get;组; }
public string Name {get;组; }
public int FavoriteChildId {get;组; }
public Child FavoriteChild {get;组; }
// public int WorstChildId {get;组; }
public Child WorstChild {get;组; }
public ICollection< Child>孩子{get;组; }
}

public class Child
{
public int Id {get;组; }
public string Name {get;组; }
// public int ParentId {get;组; }
public Parent Parent {get;组; }
}

public class CFContext:DbContext
{
public DbSet< Parent>父母{get;组; }
public DbSet< Child>孩子{get;组; }
}

如果未指定外键名称,则不起作用方式来编辑模型。有谁知道如何指定外键名称?

解决方案

按照命名约定将创建正确的FK以上 - 您的代码:

  public int WorstChildId {get;组; } 
public Child WorstChild {get;组;

为WorstChild创建WorstChildId的FK。但是,当我尝试了上面的代码时,我得到了一个多重删除路径错误(父 - > WorstChild - > ChildTable,Parent - > FavoriteChild - > ChildTable)



您可以设置一个或两个映射,以便在删除时无级联,这样可以解决您的问题:

  public class CFContext:DbContext 
{
public DbSet< Parent>父母{get;组; }
public DbSet< Child>孩子{get;组;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.WorstChild)
.WillCascadeOnDelete(false);

modelBuilder.Entity< Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.FavoriteChild)
.WillCascadeOnDelete(假);
}
}


The following code creates foreign key errors when all code is not commented.

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int FavoriteChildId { get; set; }
    public Child FavoriteChild { get; set; }
    //public int WorstChildId { get; set; }
    public Child WorstChild { get; set; }
    public ICollection<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    //public int ParentId { get; set; }
    public Parent Parent { get; set; }
}

public class CFContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }
}

It works if the foreign key names aren't specified but then there's no way to edit the models. Does anyone know how to specify the foreign key names?

解决方案

Following the naming convention will create the proper FK's above - your code:

public int WorstChildId { get; set; }
public Child WorstChild { get; set; }

Does create the FK of WorstChildId for WorstChild. However, when I tried the code you had above, I got a multiple delete path error (Parent -> WorstChild -> ChildTable, Parent -> FavoriteChild -> ChildTable)

You can set either one, or both of your mappings to not cascade on delete, and that will fix your problem:

public class CFContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Child>()
            .HasRequired(c => c.Parent)
            .WithRequiredPrincipal(p => p.WorstChild)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Child>()
         .HasRequired(c => c.Parent)
         .WithRequiredPrincipal(p => p.FavoriteChild)
         .WillCascadeOnDelete(false);
    }
}

这篇关于代码优先循环引用外键配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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