实体框架可选:可选的关系 [英] Entity Framework optional:optional relationship

查看:174
本文介绍了实体框架可选:可选的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个可选的实体框架模型:可选的关系:



在我的情况,有时是AdditionalData记录存在指向BaseTable记录,但有时BaseTable或AdditionalData记录存在没有任何联系。该外键BaseTable(如果存在的话)是在AdditionalData表。



我希望能够导航来回BaseTable以及可能是任何AdditionalDatas之间连接。

  BaseTable 0..1 ----- 0..1 AdditionalData1 
\
---电0..1 AdditionalData2


 公共类BaseTable {
公众诠释标识{搞定;组; }
公共虚拟AdditionalType1 AdditionalType1 {搞定;组; }
公共虚拟AdditionalType2 AdditionalType2 {搞定;组; }
}

公共类AdditionalType1 {
公众诠释标识{搞定;组; }
公众诠释? BaseTableId {搞定;组; }
公共虚拟BaseTable BaseTable {搞定;组; }
}

公共类AdditionalType2 {
公众诠释标识{搞定;组; }
公众诠释? BaseTableId {搞定;组; }
公共虚拟BaseTable BaseTable {搞定;组; }
}



我如何使这项工作?我得到尽可能:

  modelBuilder.Entity< AdditionalType1>()
.HasOptional(ZMT => ZMT .BaseTable)
.WithOptionalDependent(ZM => zm.AdditionalType1)
.MAP(A => a.MapKey(BaseTableId));
modelBuilder.Entity< AdditionalType2>()
.HasOptional(ZMT => zmt.BaseTable)
.WithOptionalDependent(ZM => zm.AdditionalType2)
.MAP(一=> a.MapKey(BaseTableId));



但它告诉我:




错误:(1564,6):错误0019:在类型每个属性名称必须是
独一无二的。属性名称BaseTableId'已经定义。




我不知道到底是什么,指的,并且不知道如何解决。



编辑:如果我取出地图/映射键条款如下建议( HTTP ://stackoverflow.com/a/8016308/237091 )我得到这个错误,而不是,当查询运行使用它:




无效的列名称BaseTable_Id'
,因为它本身映射到BaseTable_Id自动代替我BaseTableId领域。



解决方案

它看起来像你试图建立一个1:从 AdditionalType 对象0..1关系(我可能误解完全。



注:我认为你将不得不按住一个BaseTableId你的 BaseTable 这个工作(或定义主键):



如果 BaseTableId 在这个实例中的外键,我觉得这可能是你以后再:

  modelBuilder.Entity< AdditionalType1>()
.HasOptional(ZMT => zmt.BaseTable)
.WithMany()
.HasForeignKey(A => a.BaseTableId);



这是我以前使用过,但不能承认完全(的了解它。 WithMany()跳我);它是从这个答案在文章中列出略有变通方法:实体框架为0..1 0关系



道歉,如果我错过了完全的地步。


I am trying to set up an Entity Framework model with an optional:optional relationship:

In my situation, sometimes an AdditionalData record exists that points to a BaseTable record, but sometimes BaseTable or AdditionalData records exist without any linkage. The foreign key to the BaseTable (if it exists) is on the AdditionalData table.

I want to be able to navigate back and forth between BaseTable and any AdditionalDatas that might be connected.

BaseTable  0..1 ----- 0..1 AdditionalData1
                 \
                  --- 0..1 AdditionalData2


public class BaseTable {
    public int Id { get; set; }
    public virtual AdditionalType1 AdditionalType1 { get; set; }
    public virtual AdditionalType2 AdditionalType2 { get; set; }
}                  

public class AdditionalType1 {
    public int Id { get; set; }
    public int? BaseTableId { get; set; }
    public virtual BaseTable BaseTable { get; set; }
}

public class AdditionalType2 {
    public int Id { get; set; }
    public int? BaseTableId { get; set; }
    public virtual BaseTable BaseTable { get; set; }
}

How do I make this work? I got as far as:

modelBuilder.Entity<AdditionalType1>()
    .HasOptional(zmt => zmt.BaseTable)
    .WithOptionalDependent(zm => zm.AdditionalType1)
    .Map(a => a.MapKey("BaseTableId"));
modelBuilder.Entity<AdditionalType2>()
    .HasOptional(zmt => zmt.BaseTable)
    .WithOptionalDependent(zm => zm.AdditionalType2)
    .Map(a => a.MapKey("BaseTableId"));

but it tells me this:

error: (1564,6) : error 0019: Each property name in a type must be unique. Property name 'BaseTableId' was already defined.

I don't know exactly what that refers to, and not sure how to fix.

EDIT: If I remove the Map/MapKey clauses as suggested here (http://stackoverflow.com/a/8016308/237091) I get this error instead, when a query runs that uses it:

Invalid column name 'BaseTable_Id' as it maps itself to BaseTable_Id automatically instead of my BaseTableId field.

解决方案

It looks like you're trying to set up a 1:0..1 relationship from your AdditionalType objects (which I may have misinterpreted completely.

N.B. I think you would have to hold a BaseTableId on your BaseTable for this to work (or define a primary key):

If BaseTableId is the foreign key in this instance, I think this may be what you're after:

modelBuilder.Entity<AdditionalType1>()
    .HasOptional(zmt => zmt.BaseTable)
    .WithMany()
    .HasForeignKey(a => a.BaseTableId);

Which is what I've used previously, but can't admit to understanding it fully (the .WithMany() trips me up); it's a slight workaround listed in article from this answer: Entity Framework 0..1 to 0 relation)

Apologies if I've missed the point entirely.

这篇关于实体框架可选:可选的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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