[英]How to refresh Entity Framework Core DbContext but keep my property decorations?
I am creating a new .NET Core application with the following components:我正在使用以下组件创建一个新的 .NET 核心应用程序:
I am using Entity Framework core for the model.我正在为 model 使用实体框架核心。 I generally hate ORM tools but time is critical for this project so I don't have much choice.
我通常讨厌 ORM 工具,但时间对于这个项目来说至关重要,所以我没有太多选择。 I have to say that, all things considered, it is pretty darn solid.
我不得不说,考虑到所有因素,它非常可靠。
One of the issues I had is that I had to decorate some of my model's properties with the JsonIgnore attribute, eg :我遇到的问题之一是我必须使用 JsonIgnore 属性来装饰我的模型的一些属性,例如:
[JsonIgnore]
public virtual ICollection<LabOrder> LabOrders { get; set; }
The reason for that is that otherwise, when I serialize the object (in order to return it from an api) .NET throws the exception about too many nested levels or whatever because of parent-child-back-to-parent relationships that would cause a never-ending recursion to happen.这样做的原因是,否则,当我序列化 object(为了从 api 返回它)时,.NET 会抛出关于太多嵌套级别的异常,或者由于父-子-回-父关系会导致永无止境的递归发生。 So, all fine and dandy, the JsonIgnore attribute did just what I need.
所以,一切都很好,JsonIgnore 属性正是我所需要的。
BUT, here is my issue.但是,这是我的问题。 I've gone through the trouble of decorating these properties everywhere I need to, and completed a first round of development and testing.
我已经经历了在任何我需要的地方装饰这些属性的麻烦,并完成了第一轮开发和测试。 I need to make some adjustments to my database model;
我需要对我的数据库 model 进行一些调整; which I've done... and now it is time to refresh the EF Core dbcontext.
我已经完成了……现在是时候刷新 EF Core dbcontext。 When creating new properties or methods I can use partial classes to retain any "custom" changes I make to the model, but I have no idea how I might accomplish that here.
创建新属性或方法时,我可以使用部分类来保留我对 model 所做的任何“自定义”更改,但我不知道如何在这里完成。
So: Is it possible for one to retain my property decorations and still use the Scaffold-dbcontext command to refresh the model?那么:是否有可能保留我的财产装饰并仍然使用 Scaffold-dbcontext 命令刷新 model? I've used that command successfully in the past, but now I need to make sure I somehow don't lose my changes.
我过去曾成功使用过该命令,但现在我需要确保我不会丢失我的更改。
I believe I have determined that this is in fact, simply not doable.我相信我已经确定这实际上是不可行的。
https://forums.asp.net/t/2139531.aspx?Loading%20MetaData%20from%20Partial%20Class%20in%20Aspnet%20Core https://forums.asp.net/t/2139531.aspx?Loading%20MetaData%20from%20Partial%20Class%20in%20Aspnet%20Core
I've read through everything I can find here, the forums, and the MS documentation.我已经阅读了所有可以在这里找到的内容、论坛和 MS 文档。 It should work, but the decorations simply aren't respected when in a ModelMetadataClass.
它应该可以工作,但是在 ModelMetadataClass 中根本不尊重装饰。 The only way to do this is to implement another layer such as a view-model and handle it all there.
做到这一点的唯一方法是实现另一个层,例如视图模型并在那里处理它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.