EF Codefirst AddOrUpdate,相关实体为标识符

时间:2018-07-23 14:26:36

标签: vb.net entity-framework

我想使用AddorUpdate通过相关实体设置标识符表达式。

我的课:

Class Task
   property id as Integer
   property name as String
End Class

我的关系班:

Class RelationTask
   property id as Integer
   property task as Task
   property parentTask as Task
End Class

现在我想添加一个像这样的关系:

taskA = new Task with {.name = "A"}
taskB = new Task with {.name = "B"}
.... ' add tasks to context
context.RelationTasks.AddOrUpdate(function(r) r.task, new RelationTask with {.task = TaskB, .parentTask = TaskA})

这不起作用(错误“ 无法创建类型为'DataModel.Task'的常量值。在此上下文中仅支持原始类型或枚举类型。“),因为我使用了实体而不是值... 我该怎么办?

还有第二个问题:我如何写相同但带有多个标识符的东西?我认为类似function(r) {r.task, r.parentTask}

谢谢!

1 个答案:

答案 0 :(得分:0)

由于没有人回答,所以我尝试自己寻找解决方案,但是我不敢相信这是最好的方法。无论如何,我添加了两个相关实体的ID,因此可以将它们用作标识符:

Class Task
   property id as Integer
   property name as String
End Class

Class RelationTask
   property id as Integer
   property task as Task
   property taskId as Integer
   property parentTask as Task
   property parentTaskId as Integer
End Class


taskA = new Task with {.name = "A"}
taskB = new Task with {.name = "B"}
.... ' add tasks to context
context.RelationTasks.AddOrUpdate(function(r) New With {r.taskId, r.parentTaskId}, new RelationTask with {.task = TaskB, .parentTask = TaskA})

这是我想要的,但是感觉并不好...