本文介绍了实体框架4.3代码第一多个多对多使用相同的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

public class User
{
    [Key]
    public long UserId { get; set; }

    [Required]
    public String Nickname { get; set; }

    public virtual ICollection<Town> Residencies { get; set; }

    public virtual ICollection<Town> Mayorships { get; set; }
}

public class Town
{
    [Key]
    public long TownId { get; set; }

    [Required]
    public String Name { get; set; }

    public virtual ICollection<User> Residents { get; set; }
    public virtual ICollection<User> Mayors { get; set; }
}



我希望EF会创建两个多使用自动创建TownResidents许多关系和TownMayors表。我似乎无法找到必要的约定或明确的注解来得到这样的结果。

I was hoping EF would create two many to many relationships using automatically created TownResidents and TownMayors table. I can't seem to find the necessary convention or explicit annotation to get this result.

相反,我得到了镇表中的两FK用户ID和两个FK TownIds在用户表。

Instead I am getting two FK UserIds in the Town table and two FK TownIds in the User table.

任何想法如何获得EF看到这两个多对多的关系?

Any ideas how to get EF to see these at two many to many relationships?

感谢

推荐答案

好吧,EF不具备某种词,这将需要确定你(可能)要语法识别算法的 User.Residencies Town.Residents 形成一对导航性能和 User.Mayorships Town.Mayors 形成第二对。因此,假设您拥有的的一对许多关系和四个导航性能属于关系之一。 (这是您在数据库表中看到四个外键的原因。)

Well, EF doesn't have some kind of word and grammar recognition algorithm which would be required to identify that you (probably) want that User.Residencies and Town.Residents form a pair of navigation properties and User.Mayorships and Town.Mayors form a second pair. Therefore it assumes that you have four one-to-many relationships and that each of the four navigation properties belongs to one of the relationships. (This is the reason for the four foreign keys you have seen in the database tables.)

本标准假设是不是你想要的,因此你必须明确定义的关系重写此标准约定:

This standard assumption is not what you want, hence you must define the relationships explicitly to override this standard convention:

无论是与数据的注解:

public class User
{
    [Key]
    public long UserId { get; set; }

    [Required]
    public String Nickname { get; set; }

    [InverseProperty("Residents")]
    public virtual ICollection<Town> Residencies { get; set; }

    [InverseProperty("Mayors")]
    public virtual ICollection<Town> Mayorships { get; set; }
}

或用流利的API:

modelBuilder.Entity<User>()
    .HasMany(u => u.Residencies)
    .WithMany(t => t.Residents)
    .Map(x =>
    {
        x.MapLeftKey("UserId");
        x.MapRightKey("TownId");
        x.ToTable("TownResidents");
    });

modelBuilder.Entity<User>()
    .HasMany(u => u.Mayorships)
    .WithMany(t => t.Mayors)
    .Map(x =>
    {
        x.MapLeftKey("UserId");
        x.MapRightKey("TownId");
        x.ToTable("TownMayors");
    });



流利的API做的好处是你可以控制的链接表的名称(和键列名以及)。您不能定义与数据的注解这些名字。

Fluent API has the advantage that you can control the name of the link table (and the key column names as well). You cannot define those names with data annotations.

这篇关于实体框架4.3代码第一多个多对多使用相同的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:33