简体   繁体   English

如何刷新 Entity Framework Core DbContext 但保留我的属性装饰?

[英]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 核心应用程序:

  1. .NET Core 3.0 Web API for apis .NET Core 3.0 Web API for api
  2. .NET Core Razor pages for the web UI web UI 的 .NET 核心 Razor 页面
  3. .NET Core class libraries - one for utilities and one for the model .NET 核心 class 库 - 一个用于实用程序,一个用于 model

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.

相关问题 如何刷新 Entity Framework Core DBContext? - How to refresh an Entity Framework Core DBContext? 如何在 Entity Framework Core 中使用复数 DbSet 属性名称搭建 DbContext? - How to scaffold DbContext with plural DbSet property names in Entity Framework Core? 如何在 Entity Framework Core 中处理 DBContext 实例化 - How to handle DBContext instancing in Entity Framework Core 如何将动态实体设置为 dbcontext Entity Framework Core? - How to set dynamic entity to dbcontext Entity Framework Core? 我的 Context 没有从 Entity Framework Core 中的 DbContext 继承 - My Context does not inherit from DbContext in Entity Framework Core 从 Entity Framework Core 中的实体获取 DbContext - Get DbContext from Entity in Entity Framework Core 如何使用 Entity Framework Core 将现有存储过程添加到 dbcontext - How to add an existing stored procedure to dbcontext with Entity Framework Core 如何将存储过程添加到 Entity Framework Core DbContext? - How to add stored procedures to Entity Framework Core DbContext? 如何在 Entity Framework Core 中将连接字符串传递给 DBContext? - How do I pass connection string to DBContext in Entity Framework Core? 如何在实体框架核心中创建DBContext作为现有事务的一部分 - How to create a DBContext as part of an existing transaction in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM