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 
相关问题