1
我基本上有三個表與FK的參考在EF的導航屬性父表
讓
- MakeId
- MakeName
型號
- ModelId
- MODELNAME
- MakeId(FK)
車輛
- VehicleId
- 購置日期
- ModelId(FK)
我想從車輛上的導航屬性使不修改我的數據庫
我嘗試:
public class Make
{
public int MakeId {get;set;}
<snip>...</snip>
public virtual Vehicle Vehicles {get;set;}
}
<snip>Model</snip>
public class Vehicle
{
public int VehicleId {get;set}
<snip>...</snip>
public virtual Make VehicleMake {get;set;}
}
public class VehicleMap : EntityTypeConfiguration<Vehicle>
{
public VehicleMap()
{
this.HasKey(t => t.VehicleId);
<snip>...</snip>
this.HasRequired(t => t.VehicleMake)
.WithMany(t => t.Vehicles)
.HasForeignKey(t => t.Model.MakeId);
}
}
但是我這樣做時,我收到異常:
屬性表達倒是=> d.Model.MakeId'是無效的。 表達式應該表示一個屬性:C#:'t => t.MyProperty' VB.Net:'Function(t)t.MyProperty'。當指定多個 屬性時,使用匿名類型:C#:'t => new {t.MyProperty1, t.MyProperty2}'VB.Net:'Function(t)New From {t.MyProperty1, t.MyProperty2}' 。
如何在我的數據庫中不添加Vehicle.ModelId列的情況下在我的車輛製造&車輛表上創建導航屬性?
這是好的,我明白這一點,但我怎樣才能讓Make.Vehicles正確映射? – 2012-07-17 15:21:10
@NathanKoop同樣的問題也是如此。你不能映射它。解決方法是查詢數據庫並在需要該屬性時手動分配數據庫。 – Eranga 2012-07-17 15:49:59
感謝您的努力,我會做一些進一步的工作,並在此發佈任何更新。 – 2012-07-17 16:23:47