简体   繁体   English

在映射期间将外键作为属性公开时发生冲突

[英]A conflict in exposing a foreign key as a property during mappings

I have the following table structure: 我具有以下表结构:

Affiliations

Id    Party1    Party2
1     100       200
2     300       400

And a table referenced through the FKs of Affiliations 并通过关联的FK引用的表格

Parties

Id    Name
100   Hank
200   Ted
300   Kim
400   Joe

Using my Party entity (not shown), I define a relationship between the tables using Fluent API: 使用我的Party实体(未显示),我使用Fluent API定义表之间的关系:

modelBuilder.Entity<Party>()
            .HasMany(c => c.LeftPartyAffiliations)
            .WithRequired()
            .Map(m => m.MapKey("Party1"));

modelBuilder.Entity<Party>()
            .HasMany(c => c.RightPartyAffiliations)
            .WithRequired()
            .Map(m => m.MapKey("Party2"));

The party entity maps left and right relationships to their respective properties, LeftPartyAffiliations and RightPartyAffiliations . 派对实体将左右关系映射到它们各自的属性LeftPartyAffiliationsRightPartyAffiliations By defining the rule above, EF automatically adds Party1 and Party2 to my Affiliations table. 通过定义以上规则,EF会自动将Party1Party2添加到“我的会员”表中。

My problem lies in the fact that while the values in each affiliation map correctly, I don't have a way of knowing what the affiliation Id on the left and right sides, as I am mapping these tables to my columns through my Party relationship. 我的问题在于,尽管每个从属关系中的值正确映射,但我无法通过Party关系将这些表映射到我的列上,所以无法知道左侧和右侧的从属ID。

For instance, I would like to be able to access the actual Party1 and Party2 Id's via my Affiliation entity: 例如,我希望能够通过我的会员实体访问实际的Party1和Party2 ID:

public class PartyAffiliation
{
    ...

    [Column("Party1")]
    public int Party1 { get; set; }

    [Column("Party2")]
    public int Party2 { get; set; }

    ...
}

...but this is impossible as I am already defining my relationship through mapping to my Left and Right collections, from Party, via Fluent API. ...但这是不可能的,因为我已经通过Fluent API通过映射到Party的Left和Right集合来定义我的关系。

A perfect situtation would allow me to do the Fluent API mappings, which would create the columns and relationships as well as allowing me to access my Party1 and Party2 Id's via the physical columns defined in my Affiliation mapping. 理想的情况是允许我执行Fluent API映射,这将创建列和关系,并允许我通过我的从属关系映射中定义的物理列访问我的Party1和Party2 ID。

If I attempt to define the properties phyically in the entity as well as in Fluent API, I get: 如果尝试在实体以及Fluent API中物理定义属性,则会得到:

Each property name in a type must be unique. 类型中的每个属性名称都必须是唯一的。 Property name was already defined 'xxx' 属性名称已定义为“ xxx”

Is there a workaround to this issue or a way of mapping a column to multiple properties without physically adding an additional field to the database? 是否有解决此问题的方法,或将列映射到多个属性而又不向数据库物理添加其他字段的方法?

You can use HasForeignKey instead of MapKey : 您可以使用HasForeignKey代替MapKey

modelBuilder.Entity<Party>()
    .HasMany(c => c.LeftPartyAffiliations)
    .WithRequired()
    .HasForeignKey(pa => pa.Party1)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<Party>()
    .HasMany(c => c.RightPartyAffiliations)
    .WithRequired()
    .HasForeignKey(pa => pa.Party2)
    .WillCascadeOnDelete(false);

It maps a property in your model class to a foreign key column in the database. 它将模型类中的属性映射到数据库中的外键列。 Disabling cascading delete is necessary here (at least for one of the two relationships), otherwise you'd have a multiple cascading delete path from Party to PartyAffiliation which is forbidden in SQL Server. 这里必须禁用级联删除(至少对于两个关系之一而言),否则您将拥有从PartyPartyAffiliation的多个级联删除路径,这在SQL Server中是被禁止的。

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

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