将Entity Framework 4.0迁移到核心

编程入门 行业动态 更新时间:2024-10-27 01:27:11
本文介绍了将Entity Framework 4.0迁移到核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个使用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迁移到核心

本文发布于:2023-11-14 03:11:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586056.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   Entity   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!