简体   繁体   English

实体框架核心 - 拥有类型的修改键

[英]Entity Framework Core - Modified key on owned type

While saving changes in my database, an exception with the following message is returned:在我的数据库中保存更改时,返回带有以下消息的异常:

The property 'OrderId' on entity type 'Order.CustomerDeliveryDetails#CustomerDetails' is part of a key and so cannot be modified or marked as modified.实体类型“Order.CustomerDeliveryDetails#CustomerDetails”的属性“OrderId”是键的一部分,因此不能修改或标记为已修改。 To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.要使用标识外键更改现有实体的主体,首先删除依赖项并调用“SaveChanges”,然后将依赖项与新主体关联。

The database is implemented with entity framework core with a 'code first' approach.该数据库是使用实体框架核心实现的,采用“代码优先”方法。 Order.CustomerDeliveryDetails is an owned type (of the type CustomerDetails ) of the entity Order . Order.CustomerDeliveryDetails是实体Order的拥有类型(属于CustomerDetails类型)。 CustomerDetails has no property called OrderId . CustomerDetails没有名为OrderId的属性。 As I understand OrderId is a implicit key , generated by entity framework core as a shadow property .据我了解, OrderId是一个隐式键,由实体框架核心作为影子属性生成。

The classes are structured as follows:这些类的结构如下:

public class Order
{
    public int Id { get; set; }
    public CustomerDetails CustomerDeliveryDetails { get; set; }
}

[Owned]
public class CustomerDetails
{
    public string Street { get; set; }
}

The object is updated as follows: object更新如下:

var order = await orderContext.Orders
        .Where(o => o.Id== updateOrder.Id)
        .FirstOrDefaultAsync();

order.CustomerDeliveryDetails.Street = updateOrder.CustomerDeliveryDetails.Street;
await orderContext.SaveChangesAsync();

What I fail to understand is how OrderId can be modified, when it can't be accessed directly in the code.我不明白的是,当无法在代码中直接访问OrderId时,如何修改它。

The only thing I can think of which might cause this error, is the fact that this update is being run on a timed webjob in Azure.我能想到的唯一可能导致此错误的是,此更新正在 Azure 中的定时网络作业上运行。 This is hunch is supported by the fact that the update passes the related unit tests.更新通过了相关的单元测试这一事实支持了这种预感。 Could this have to do with a race condition ?这可能与比赛条件有关吗?

Update:更新:

I'm fairly certain the error comes from some sort of race condition.我相当确定错误来自某种竞争条件。 The timed webjob loads a list of orders that need to be updated every 2 minutes.定时网络作业会加载需要每 2 分钟更新一次的订单列表。 The update works fine as long as the list contains less then +-100 orders, but starts to fail once this list gets longer.只要列表包含少于 +-100 个订单,更新就可以正常工作,但一旦此列表变长,更新就会开始失败。

The webjob is probably inable to finish updating all the orders within 2 minutes if the list gets to long.如果列表过长,webjob 可能无法在 2 分钟内完成所有订单的更新。

The context is added through dependency injection as follows:通过依赖注入添加上下文,如下所示:

serviceProvider.AddDbContext<OrdersContext>(options => options.UseSqlServer(ctx.Configuration["ConnectionString"], sqlOptions => sqlOptions.EnableRetryOnFailure()));

My best geuss is that the context is being shared between multiple calls of the webjob, which is causing the errors.我最好的猜测是上下文正在网络作业的多个调用之间共享,这导致了错误。

This boils down to your database relationships.Are you using database first or code first approach?这归结为您的数据库关系。您是使用数据库优先还是代码优先方法? How are the models defined?模型是如何定义的? Whats the relationship between Order, CustomerDetails and the CustomerDeliveryDetails tables? Order、CustomerDetails 和 CustomerDeliveryDetails 表之间的关系是什么? Please provide the code and I will be able to help you with the solution.请提供代码,我将能够帮助您解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core中已修改实体的拥有类型属性不存在 - Owned type property not persisting for a modified entity in EF Core Entity Framework Core:具有导航属性的自有类型 - Entity Framework Core: Owned type that has navigation properties 与 IdentityUser 一起使用时,Entity Framework Core 拥有的类型值 Object 抛出 required Primary Key to be defined 错误。 为什么? - Entity Framework Core Owned Type Value Object throws required Primary Key to be defined error when used with IdentityUser. Why? 如何使用实体框架核心 3.1.1 为拥有的实体类型设置复合主键? - How to set the composite primary key for owned entity types using entity framework core 3.1.1? 实体框架核心:将拥有的属性与继承相结合 - Entity Framework Core: Combine owned properties with inheritance Entity Framework Core 3.0中的拥有类型问题 - Owned types problem in Entity Framework Core 3.0 Entity Framework Core 在使用自有类型时尝试加入不存在的表 - Entity Framework Core tries to join to a non-existent table when using an owned type 如何使用 Entity Framework Core 3.0 创建所需的拥有类型 - How can I create a Required Owned Type with Entity Framework Core 3.0 实体类型的属性是键的一部分,因此无法进行修改或将其标记为已修改。 EF核心Dotnet核心 - The property on entity type is part of a key and so cannot be modified or marked as modified. EF Core Dotnet Core 实体框架核心设置在添加时拥有实体null - Entity Framework Core setting owned entity null when added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM