需要实体框架忽略模型成员



我们需要存储从另一个与我们的模型相关的表中提取的数据列表。(我们太深入构建,无法将此关系添加到我们的数据库中。当我们尝试在需要它的模型的属性中加载此列表时。

我们将此列表加载到视图中的下拉列表中。尝试将此列表加载到控制器中的模型中时发生错误。真正需要注意的是,我们在视图中的表中为每条记录都有一个唯一的下拉列表。

我们的班级:

Public class OurModel
  public property ID As Integer
  Public property First As Integer
  Public property Last As Integer
  Public property myList As List(Of SelectListItem)//This is our problem member
End class

控制器:

                         //This ViewModel is what we pass around from page to page to populate the various elements we need that is not tied to a specific model
Public Function LoadList(myViewModel As ViewModel) As Action Result
   Using db //our database resource 
    For i As Integer = 0 to myViewModel.OurModel
       //Assume table select statement previously declared and initialized into dbGet
       **NOTE: dbGet is retieving data from a different table that is tied to this Model**
       dbGet = dbGet.Where(Function(x) x.ID = myViewModel.OurModel.ID)
       myViewModel.OurModel.myList = dbGet.ToList().[Select](Function(x) New SelectListItem() With {.Value = x.number, .Text = x.number})//ERROR IS HERE
    Next
   End Using
End Function

错误:

LINQ to Entities 无法识别方法 'DB。模型表 get_Item(Int32)' 方法,并且此方法不能转换为存储表达式。

更新:问题似乎是 LINQ 正在尝试使用我之前在此控制器中执行的查询的 DBContext 执行某些操作。该错误引用DB.Employee而不是DB.Position这是我正在查询的内容。

我在 VB.Net 中几乎没有做任何事情,但听起来get_Item(Int32)可能与C#中的索引器相同。

有一个众所周知的缺点,即实体框架不支持包含索引器的类(即使使用 NotMapped 修饰该属性)。

我面临着类似的问题,那就是实现可锁定的可观察集合。 我发现由于 EF 中的索引器问题,我无法实现IList<T>,但我可以实现ICollection<T>

锁定/解锁可观察集合

如果可以将列表的语义替换为集合的语义,则应该可以解决此问题。

我最终不得不编写连接到实体框架外部数据库的新类。如果要使用实体框架,请确保在开始生成应用程序之前数据库设计完美,祝您好运。

最新更新