【发布时间】:2018-10-26 01:54:08
【问题描述】:
当我尝试为我的数据库播种时,我使用的是这样的匿名类型:
context.CalibrationTargets.AddOrUpdate(x => new { calId = x.CalibrationSetup.Id, targetIndex = x.TargetIndex },
new CalibrationTarget()...
但是因为CalibrationSetup 是一个虚拟属性,我似乎得到了错误:
属性表达式
x => new <>f__AnonymousType102(calId = x.CalibrationSetup.Id, targetIndex = x.TargetIndex)无效。该表达式应表示一个属性:C#:t => t.MyPropertyVB.Net:Function(t) t.MyProperty。指定多个属性时使用匿名类型: C#:t => new { t.MyProperty1, t.MyProperty2 }VB.Net: `Function(t) New With { t.MyProperty1, t.MyProperty2 }
同样在阅读了这篇The properties expression is not valid. The expression should represent a property 之后,问题似乎是因为CalibrationSetup 是virtual 属性。
有没有一种方法可以解决这个问题,而不必使用另一个 Calibration_Id 列而不是 virtual 来填充数据库以在匿名类型中使用?
【问题讨论】:
-
你试过
x => new { x.CalibrationSetup.Id, x.TargetIndex }吗? -
@tschmit007 是的,使用它得到同样的错误
-
所以 CalibrationSetup 是 CalibrationTarget 的子对象?在父级中公开 FK 并使用它 (CalibrationTarget.CalibrationSetup_Id)。
-
@SteveGreene CalibrationTarget 是 CalibrationSetup 的子对象
标签: c# entity-framework entity-framework-6 anonymous-types