简体   繁体   English

实体框架设置导航属性为零与零的关联为null

[英]Entity Framework setting navigation property null in zero to one association

I have a Zero to one association between two tables. 我在两个表之间有一个零对一的关联。 For instance, I have entity Car and Entity License and a car can have 0 or 1 License. 例如,我有实体Car和实体License ,一辆汽车可以有0或1个许可证。
(A car can have one license, but a license can have 0 or 1 car) When I try to remove a car's license, by setting Car.License = null i get an exception such as: (一辆汽车可以拥有一个执照,但是一个执照可以拥有0或1辆汽车)当我尝试通过设置Car.License = null来删除汽车的执照时,出现了Car.License = null异常:

A relationship from the 'Car_Licence' AssociationSet is in the 'Deleted' state. “ Car_Licence” AssociationSet中的关系处于“已删除”状态。 Given multiplicity constraints, a corresponding 'License' must also in the 'Deleted' state. 给定多重性约束,相应的“许可证”也必须处于“已删除”状态。

I want the license to stay in the main Licenses DbSet, and i want it's reference to Car to remain, all I want it that the Car's license be set to null. 我希望许可证保留在主许可证DbSet中,并且希望保留对Car的引用,我只想将Car的许可证设置为null。

What am i missing? 我想念什么?

Setting Car.License to null conflicts with association you have set, where car must have one license set. Car.License设置为null与您设置的关联冲突,在该关联中,car必须设置一个许可证。 You need to change your association so that car can have one or zero licenses for it to work. 您需要更改关联,以便汽车可以拥有一个或零个许可证才能使用。

EDIT 编辑

You will have to set both ends of association to zero or one if you want to null the property. 如果要使属性为空,则必须将关联的两端都设置为零或一。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM