VB.NET示例实体框架4.2代码优先实体拆分
实体拆分:一个类、两个或多个表 这是如何在C#中完成的,但我需要让它在vb.net中工作。VB.NET示例实体框架4.2代码优先实体拆分,vb.net,entity-framework,entity-framework-4,ef-code-first,code-first,Vb.net,Entity Framework,Entity Framework 4,Ef Code First,Code First,实体拆分:一个类、两个或多个表 这是如何在C#中完成的,但我需要让它在vb.net中工作。 还有一件事:类名和表列不匹配,所以我也必须能够映射出来 我必须这样做,因为我现在工作的地方只有vb.net商店,数据库模式是fubar,但是他们有太多(数百万)行代码直接针对asp经典版、vb.net、,而asp.net webforms则表示,现在更改模式在现实中是不可能的 protected override void OnModelCreating(DbModelBuilder modelBuild
还有一件事:类名和表列不匹配,所以我也必须能够映射出来 我必须这样做,因为我现在工作的地方只有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");
});
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Map(m=>
{
m、 属性(p=>new{p.Title,p.Content});
m、 可转帐(“员额”);
})
.Map(m=>
{
m、 属性(p=>new{p.Photo});
m、 ToTable(“后期照片”);
});
}
这是上面的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)
这里是接受答案的正确版本(使用匿名类型)。我希望这会有帮助
您确实可以通过点表示法来实现,但是代码缩进真的很奇怪。我更喜欢另一种方式:创建一个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
对于DB表,您真的必须有2个类吗?我是说邮寄和拍照。使用匿名类是不可能的吗?