如何映射这两个生成的类之间的关系?

时间:2014-07-16 12:03:13

标签: vb.net entity-framework asp.net-mvc-5

我曾经使用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

1 个答案:

答案 0 :(得分: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

这篇文章引用了一个使用Code First定义此FK关系的简单示例,示例在C#中,但应该很好地转换。