问题描述
我有一个使用Entity Framwork 4.0开发的旧项目,该项目使用了一些复杂的类型.由于需要将其迁移到.NET Core,因此我创建了一个新项目,安装了所有必需的库并使用了命令
I have an old project developed with the Entity Framwork 4.0 which uses some complex types. Since I need to migrate it to .NET Core, I created a new project, installed all required libraries and used the command
PM> Scaffold-DbContext "Server=ServerInstance; Database=DBName; Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
在现有数据库上
以便创建新模型.问题在于它不会生成复杂的类型类.
on the existing database in order to create the new models. The problem is that it does not generate the complex type classes.
我可以想象用人工创建的复杂类型替换生成的属性,使用 [ComplexType]
属性,并使用 OwnsOne
命令设置属性,但是如果有一种自动生成选项,就会徘徊.
I can imagine I could replace the generated properties with by hand created complex types, use the [ComplexType]
attribute and set the property using OwnsOne
command, but I was wandering if there is a sort of auto generation option.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
我创建了第二个局部类,以便将自定义内容添加到所创建的局部类中.还添加了部分OnModelCreatingPartial方法,在其中定义了我的复杂类型.
I created a second partial class in order to add custom stuff to the created one. Added also a partial OnModelCreatingPartial method in which I define my complex types.
以下是代码段:
[Owned]
public class MyComplexType
{
public bool AField { get; set; }
public string BField { get; set; }
}
public partial class MyMainEntity
{
public MyComplexType MyComplexType { get; set; }
}
public partial class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("myConnectionString",
opts => opts.CommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds)
);
}
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyMainEntity>().OwnsOne(e => e.MyComplexType, myComplexType =>
{
myComplexType.Property(p => p.AField).HasColumnName("ColumnNameFieldA");
myComplexType.Property(p => p.BField).HasColumnName("ColumnNameFieldB");
});
}
}
这篇关于将Entity Framework 4.0迁移到核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!