我讀了Julie Lerman的一些帖子,在這裏試圖找出如何配置2個實體之間的關係。我使用EF Code First從一個現有的數據庫生成類,我無法控制這個數據庫,並且已經有很多數據。在一張表中,PostId
是,而PostRowID
是PK
。當我修改這些類以嘗試獲得關係時,我最終得到了錯誤Invalid object name 'dbo.be_PostTagbe_Posts'.
大多數(如果不是所有的)例子似乎都是處理Code First而不是DB中的Code First。我如何映射這兩個類之間的關係?或者我需要第三張表來映射關係?如何配置EF Code First from Database中現有數據庫中的類之間的關係?
我也看了看流暢的API,並走到這一步:
modelBuilder.Entity(Of be_Posts)().HasMany(Function(a)
a.be_PostTag).WithMany().Map(Function(x)
x.MapLeftKey("PostID")x.MapRightKey("PostID") 'Don't know what to do next
我試圖使用Include
語法時出現這種情況。我添加了2個ICollection屬性,但我懷疑由於數據庫偏離EF慣例,這就是爲什麼EF無法找出關係。
Partial Public Class be_Posts
<Key>
Public Property PostRowID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
'Other Stuff
Public Property be_PostTag As ICollection(Of be_PostTag)
End Class
而且
Partial Public Class be_PostTag
<Key>
Public Property PostTagID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
'Other stuff
Public Property be_Posts As ICollection(Of be_Posts)
End Class
而且包括:
Using db As New BetterBlogContext
post = db.be_Posts.OrderByDescending(Function(x)
x.DateCreated).Include("be_PostTag").Where(Function(p) p.PostRowID
= id).FirstOrDefault
End Using
這在db上看起來像什麼?有沒有連接表? – Jasen
標籤和帖子沒有連接表。 be_PostTag將PostTagID設置爲int,將PostID設置爲uniqueidentifier,將BlogID設置爲uniqueidentifier,將PostID設置爲uniqueidentified並將其標記爲NVARCHAR(50)' –
使用現有代碼映射多對多關係,以便名爲「be_PostTagbe_Posts」的連接表是期待。這兩個實體之間的預期關係是什麼? – Jasen