Is it possible to make Entity Framework separate scaffolding mapping into multiple classes, instead of auto-generating all mapping into a single file?

huangapple go评论87阅读模式
英文:

Is it possible to make Entity Framework separate scaffolding mapping into multiple classes, instead of auto-generating all mapping into a single file?

问题

我正在运行 Entity Framework 的脚手架命令,自动基于我的数据库 DatabaseName 创建类:

Scaffold-DbContext 
    "Data Source=COMPUTER; Initial Catalog=DatabaseName; Persist Security Info=True; User ID=USER; Password=PASSWORD; trustServerCertificate=true;" 
    MicrosoftEntityFrameworkCore.SqlServer -OutputDir Models -force

然而,我注意到 Entity Framework 将所有映射都放在一个名为 DatabaseNameContext.cs 的上下文文件中。对我来说,这意味着将 5000 多行代码的映射都放在一个文件中,这不如我希望的那样易读和清晰(分开)。

我理想情况下希望在项目下创建一个名为 Mapping 的文件夹,并为每个表创建一个映射文件,像这样:

UserMapping.csPurchase.csProduct.cs 等,在每个文件中包含与单个表相关的映射。

是否有一种自动使用 Entity Framework 完成此操作的方法,还是我必须手动操作才能实现这个结果?谢谢。

英文:

I am running the Scaffolding command for Entity Framework to automatically create classes based on my database DatabaseName:

Scaffold-DbContext 
    "Data Source=COMPUTER; Initial Catalog=DatabaseName; Persist Security Info=True; User ID=USER; Password=PASSWORD; trustServerCertificate=true;" 
    MicrosoftEntityFrameworkCore.SqlServer -OutputDir Models -force

However, I am noticing that Entity Framework puts ALL of the mapping in that single context file DatabaseNameContext.cs. For me this means 5000+ lines of code of mapping all in one file, and it's just not as readable and 'clean' (separated) as I would like.

What I would ideally want is to create a Mapping folder under my project and have a map file per table, like so:

UserMapping.cs, Purchase.cs, Product.cs etc where inside of each is the relevant mapping for that single table.

Is there a way to do this automatically using Entity Framework, or would I have to manually do this to achieve this result? Thank you.

答案1

得分: 1

指定 -ContextDir-OutputDir 分别应该给您所需的结果。如果需要的话,它们可以是相同的实际值。您还可以使用 EF Core Power Tools 如果您更喜欢图形用户界面。这还提供了一些 EF Core 不提供的功能,比如生成存储过程的脚手架。

英文:

Specifying -ContextDir and -OutputDir separately should give you the desired result. They can be the same actual value if needs be. You can also use EF Core Power Tools if you prefer a GUI. This also provides some features that EF Core doesn't, like scaffolding of Stored Procedures.

huangapple
  • 本文由 发表于 2023年7月11日 08:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定