实体拆分:一个类,两个或多个表。
这是如何在C#中完成的,但是我需要让它在vb.net中工作。
还有一件事:类名和表列不匹配,所以我也必须能够映射出来。
我必须这样做,因为我现在工作的地方是一个只有vb.net 的商店,而数据库模式是fubar,但是他们有那么多(数百万)行代码直接在asp、vb.net和asp.net的get表单中针对数据库完成了,所以现在更改模式是不可能的。
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");
});
}发布于 2011-12-21 18:20:41
这是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)发布于 2014-07-18 13:08:07
这里是接受答案的正确版本(带有匿名类型用法)。我希望这会有帮助..。
您确实可以通过点表示法来完成它,但是代码缩进确实很奇怪。我更喜欢另一种方式:创建一个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 Subhttps://stackoverflow.com/questions/8499321
复制相似问题