2014-07-16 82 views
0

我以前用EF Code First From Database生成基于现有数据库的类。我需要映射这两个类之间的关系。我怀疑我必须设置外键,但不要设置外键。如何映射这两个生成的类之间的关系?

Partial Public Class be_Posts 
    <Key> 
    Public Property PostRowID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    <StringLength(255)> 
    Public Property Title As String 
    Public Property Description As String 
    Public Property PostContent As String 
    Public Property DateCreated As Date? 
    Public Property DateModified As Date? 
    <StringLength(50)> 
    Public Property Author As String 
    Public Property IsPublished As Boolean? 
    Public Property IsCommentEnabled As Boolean? 
    Public Property Raters As Integer? 
    Public Property Rating As Single? 
    <StringLength(255)> 
    Public Property Slug As String 
    Public Property IsDeleted As Boolean 
End Class 



Partial Public Class be_PostTag 
    <Key> 
    Public Property PostTagID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    Public Property PostRowID As Integer 
    <StringLength(50)> 
    Public Property Tag As String 
End Class 

回答

0

看起来很简单,但我可能会错过一些东西。有一段时间没有在VB编码,所以原谅任何错别字。

您需要在两个表的PostRowID值上添加一个外键关系。

我相信你完成后应该看起来像这样。

Partial Public Class be_Posts 
    <Key> 
    Public Property PostRowID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    <StringLength(255)> 
    Public Property Title As String 
    Public Property Description As String 
    Public Property PostContent As String 
    Public Property DateCreated As Date? 
    Public Property DateModified As Date? 
    <StringLength(50)> 
    Public Property Author As String 
    Public Property IsPublished As Boolean? 
    Public Property IsCommentEnabled As Boolean? 
    Public Property Raters As Integer? 
    Public Property Rating As Single? 
    <StringLength(255)> 
    Public Property Slug As String 
    Public Property IsDeleted As Boolean 
End Class 



Partial Public Class be_PostTag 
    <Key> 
    Public Property PostTagID As Integer 
    Public Property BlogID As Guid 
    Public Property PostID As Guid 
    Public Property PostRowID As Integer 
    <StringLength(50)> 
    Public Property Tag As String 
    <ForeignKey("PostRowID")> 
    Public Property PostRow As be_Posts 
End Class 

下面是引用定义与代码首先这个FK关系的一个简单的例子的文章,该例子是在C#中,但应该翻译得相当好。

+0

谢谢你的回答。但它没有工作。我将 Public Property PostRow添加为be_Posts为be_PostTag,并在be_Posts中将添加到PostRowID。然后抛出错误该属性不能配置为导航属性。该属性必须是有效的实体类型。 –

+0

@AshokPadmanabhan这里有一篇类似的文章,其答案就是提供了很好的信息和例子,你能否看到这些例子是否为你的新问题提供了答案。再次抱歉,这些例子是在C#中。 http://stackoverflow.com/questions/20202578/relationships-in-entity-framework-code-first – Madullah

相关问题