英文:
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.cs
、Purchase.cs
、Product.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论