1

實體拆分:一個類,兩個或多個表。VB.NET示例實體框架4.2代碼第一個實體拆分

這是如何在C#中完成的,但我需要讓它在vb.net中工作。
還有一件事:類名和表列不要匹配,所以我也必須能夠映射出來。

我必須得到這個工作,這樣一來,因爲我在現在工作的地方是一個vb.net 店和數據庫架構是FUBAR,但他們有這麼多(百萬)的代碼行直接對數據庫在asp經典,vb.net和asp.net webforms現在改變架構是不現實的可能。

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Post>() 
    .Map(m => 
     { 
     m.Properties(p => new { p.Title, p.Content }); 
     m.ToTable("Posts"); 
     }) 
    .Map(m => 
     { 
     m.Properties(p => new { p.Photo }); 
     m.ToTable("PostPhotos"); 
     }); 
} 

回答

3

這是VB相當於上述的:

modelBuilder.Entity(Of Post)().Map(Sub(m) 
              m.Properties(Of Post)(
              Function(p) _ 
               New Post With {.Title= p.Title, _ 
                   .Content = p.Content }) 
              m.ToTable("Posts") 
             End Sub).Map(Sub(m) 
                 m.Properties(
                  Function(p) _ 
                  New Customer With {.Photo = p.Photo}) 
                 m.ToTable("PostPhotos") 
                End Sub) 
+0

你真的必須有2班數據庫表?我的意思是郵政和照片。用匿名類來做它是不可能的? – 2014-07-18 09:42:05

0

這裏一個正確的版本(與匿名類型的使用)接受的答案的。我希望這會有所幫助...

你確實可以通過點符號來實現,但代碼縮進真的很奇怪。我更喜歡另一種方法:創建一個EntityTypeConfiguration

Public Class PostConfiguration 
    Inherits EntityTypeConfiguration(Of Post) 

    Public Sub New() 

     Map(Sub(m) 
       m.Properties(
        Function(p) _ 
         New With {Key p.Title, Key p.Content}) 
       m.ToTable("Posts") 
      End Sub) 

     Map(Sub(m) 
       m.Properties(
        Function(p) _ 
        New With {Key p.Photo }) 
       m.ToTable("PostPhotos") 
      End Sub) 

    End Sub 
    End Class 

你只需要這個配置添加到模型中,像這樣:

Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder) 
     modelBuilder.Configurations.Add(New PostConfiguration) 
    End Sub