【发布时间】:2017-06-27 04:44:13
【问题描述】:
我的模型:
class FooEntity
{
[Key]
int Id { get; set; }
[ForeignKey("Bar"), Column("other_id")]
int OtherId { get; set; } // <-- This should be the FK
virtual BarEntity Bar { get; set; }
}
class BarEntity
{
[Key]
int Id { get; set; }
[Column("other_id")]
int OtherId { get; set; } // <-- This is the other side of the FK
}
基本上我想重现这个 SQL:
select *
from foo f
left join bar b on b.other_id = f.other_id -- AND other conditions to "guarantee" uniqueness
但是使用模型构建配置:
modelBuilder.Entity<FooEntity>()
.HasOptional(f => f.Bar)
.WithRequired()
.Map(m => m.MapKey("other_id"))
.WillCascadeOnDelete(false);
我最终得到错误:“类型中的每个属性名称必须是唯一的。属性名称 'other_id' 已定义。”但是当我添加时:
modelBuilder.Entity<BarEntity>().Ignore(b => b.OtherId);
在其他配置之前,我收到错误消息:“LINQ to Entities 不支持指定的类型成员 'OtherId'。仅支持初始化程序、实体成员和实体导航属性。”
那么我怎样才能让它工作呢?更改底层数据结构绝对不是一种选择。
【问题讨论】:
标签: c# entity-framework linq