您可以在实体框架中的自引用实体中定制外键名称吗? [英] Can you customise foreign key names in self referencing entities in entity framework?

查看:185
本文介绍了您可以在实体框架中的自引用实体中定制外键名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中的一些实体具有4个审计属性:

  public virtual DateTime WhenAdded {get;组; } 

public virtual DateTime? WhenUpdated {get;组; }

public virtual User AddedBy {get;组; }

public virtual User UpdatedBy {get;组; }

我正在使用代码第一种方法,并具有以下扩展方法来映射用户属性: / p>

  public static void MapAuditFields< T>(此EntityTypeConfiguration&T;配置)其中T:class,IAuditable 
{
configuration.HasOptional< User>(e => e.AddedBy)
.WithOptionalDependent()
.Map(a => a.MapKey(AddedByUserId));
configuration.HasOptional< User>(e => e.UpdatedBy)
.WithOptionalDependent()
.Map(a => a.MapKey(UpdatedByUserId));
}

这在大多数情况下都工作正常,但不在User类上,当然与本身有递归关系。我已经看到互联网上的各种帖子,建议实体框架在这种情况下尝试自定义连接表列名称时出现错误,例如:



首先引用多对多递归关系代码实体框架





http://social.msdn.microsoft.com/Forums/en-US/f058097d-a0e7-4393-98ef-3b13ab5b165d/code-first-sequence-contains-more-than-one-matching-element -exception-when-generate-schema?forum = adonetefx



我得到的错误是Sequence包含多个匹配元素。



有人知道吗如果这已经在实体框架6中修复了?



非常感谢。

解决方案

使用 WithMany()而不是 WithOptionalDependent(),因为用户可以添加或更新多个其他用户

类:

  public class User 
{
public int Id {get;组; }
public virtual User AddedBy {get;组; }

public virtual User UpdatedBy {get;组;
}

流利的API调用:

  modelBuilder.Entity< User>()
.HasOptional(u => u.AddedBy)
.WithMany()
.Map (fkamc => fkamc.MapKey(AddedByUserId));

modelBuilder.Entity< User>()
.HasOptional(u => u.UpdatedBy)
.WithMany()
.Map(fkamc => fkamc.MapKey(UpdatedByUserId));

结果:




Some of the entities in my application have 4 audit properties on them:

    public virtual DateTime WhenAdded { get; set; }

    public virtual DateTime? WhenUpdated { get; set; }

    public virtual User AddedBy { get; set; }

    public virtual User UpdatedBy { get; set; }

I am using a code first approach and have the following extension method to map the user properties:

    public static void MapAuditFields<T>(this EntityTypeConfiguration<T> configuration) where T : class, IAuditable
    {
        configuration.HasOptional<User>(e => e.AddedBy)
            .WithOptionalDependent()
            .Map(a => a.MapKey("AddedByUserId"));
        configuration.HasOptional<User>(e => e.UpdatedBy)
            .WithOptionalDependent()
            .Map(a => a.MapKey("UpdatedByUserId"));
    }

This is working fine in most cases, but not on the User class, which of course has a recursive relationship with itself. I have seen various posts on the internet suggesting that entity framework has a bug when you try to customise join table column names in this scenario, for example:

Self-referencing many-to-many recursive relationship code first Entity Framework

and

http://social.msdn.microsoft.com/Forums/en-US/f058097d-a0e7-4393-98ef-3b13ab5b165d/code-first-sequence-contains-more-than-one-matching-element-exception-when-generating-schema?forum=adonetefx

The error I am getting is "Sequence contains more than one matching element".

Does anyone know if this has been fixed in entity framework 6?

Many thanks.

解决方案

Use WithMany() instead of WithOptionalDependent() as a user can add or update multiple other users

Class:

public class User
{
    public int Id { get; set; }
    public virtual User AddedBy { get; set; }

    public virtual User UpdatedBy { get; set; }
}

Fluent API calls:

        modelBuilder.Entity<User>()
            .HasOptional( u => u.AddedBy )
            .WithMany()
            .Map( fkamc => fkamc.MapKey( "AddedByUserId" ) );

        modelBuilder.Entity<User>()
            .HasOptional( u => u.UpdatedBy )
            .WithMany()
            .Map( fkamc => fkamc.MapKey( "UpdatedByUserId" ) );

Results:

这篇关于您可以在实体框架中的自引用实体中定制外键名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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