在实体框架中,我通常会执行以下操作:
modelBuilder.Entity(Of Model).HasKey(Function(item As Model) New With {item.PropertyA, item.PropertyB })
映射复合主键
我需要编写一个通用函数,如:
modelBuilder.Entity(Of TModelo).HasKey( MakeLambda({“PropertyA”, “PropertyB” })
Private Function MakeLambda(Of TModelo)(nameProperties As String()) As Expression(Of Func(Of TModelo, Object))
Dim type = GetType(TModelo)
Dim listProperties As New List(Of Expression)
Dim parameter = Expression.Parameter(type, "item")
For Each n As String In nameProperties
Dim refProperty = type.GetProperty(n)
listProperties.Add(Expression.MakeMemberAccess(parameter, refProperty))
Next
Dim arrayInit = Expression.NewArrayInit(GetType(Object), listProperties)
此时系统无法创建新表达
Dim newExpression = Expression.Lambda(Of Func(Of TModelo, Object))(arrayInit)
Return newExpression
End Function
可能有人有另一个解决这个问题的方法
答案 0 :(得分:0)
这样做。 dynamic newExpression = Expression.Lambda>(arrayInit,parameter);
但这对我来说还不行。 我需要这样的东西......
HasKey(p => new { p.FAMILY, p.CACHE_FAMILY, p.CUSTOMER_CODE, p.CCC, p.OPERATION, p.EVAL_CODE, p.VDT_FLAG, p.TEST_PLATFORM, p.PCBA_VENDOR });