Entity framework EF代码优先外键推理问题
我第一次使用EF代码,我很难让它推断我的类型之间的关系。鉴于这两种类型:Entity framework EF代码优先外键推理问题,entity-framework,code-first,Entity Framework,Code First,我第一次使用EF代码,我很难让它推断我的类型之间的关系。鉴于这两种类型: <Table("grpGroupType")> Public Class GroupType <Key()> Public Property GroupTypeID As Integer <Required()> Public Property IsActive As Boolean <Required()> <MaxLength(100)&
<Table("grpGroupType")>
Public Class GroupType
<Key()>
Public Property GroupTypeID As Integer
<Required()>
Public Property IsActive As Boolean
<Required()>
<MaxLength(100)>
Public Property Description As String
Public Overridable Property GroupDefinitions() As ICollection(Of GroupDefinition)
End Class
似乎没有太多关于键推理的文档,但我确实发现了这一点,而且我的类似乎遵循自动推理的规则
有人能给我指出正确的方向吗?试试添加
Public Property GroupTypeID As Integer
到您的GroupDefinition类
尽管不需要,而且在早期版本中也不需要,但似乎CTP5版本的EF需要一个更明确的定义,以便它能够处理关系。我个人希望他们在RTM之前解决这个问题。我已经解决了。我有一个SelectOne方法在本地创建DbContext。我在调用代码中创建了上下文并将其传递给SelectOne,现在一切正常。谢谢大家。请显示无法正常工作的代码。谢谢
Public Class PD
Inherits DbContext
Public Property GroupDefinitions As DbSet(Of GroupDefinition)
Public Property GroupTypes As DbSet(Of GroupType)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As ModelConfiguration.ModelBuilder)
modelBuilder.Entity(Of GroupDefinition)().HasKey(Function(b) b.GroupDefinitionID)
modelBuilder.Entity(Of GroupType)().HasKey(Function(b) b.GroupTypeID)
End Sub
End Class
Public Property GroupTypeID As Integer