[英]The relationship could not be changed because one or more of the foreign-key properties is non-nullable
Exception : 例外情况:
System.InvalidOperationException: The operation failed: The relationship could not
be changed because one or more of the foreign-key properties is non-nullable. When
a change is made to a relationship, the related foreign-key property is set to a
null value. If the foreign-key does not support null values, a new relationship
must be defined, the foreign-key property must be assigned another non-null value,
or the unrelated object must be deleted.
I have seen some solutions for the above issue as shown below.But none of them worked for me :( May be due to those solutions are for the EF 4.x versions.My app's EF version is 6.x. 我已经看到了上述问题的一些解决方案,如下所示,但是它们都不适合我:(可能是由于这些解决方案适用于EF 4.x版本。我的应用程序的EF版本是6.x.
Solution 1 and Solution 2 解决方案1和解决方案2
[Table("IpTaxMapLots")]
public class TaxMapLot : FullAuditedEntity
{
public const int MaxLength = 50;
[Required]
[MaxLength(MaxLength)]
public virtual string District { get; set; }
[ForeignKey("PropertyId")]
public virtual Property Property { get; set; }
public virtual int PropertyId { get; set; }
}
[Table("IpProperties")]
public class Property : FullAuditedEntity
{
public const int MaxLength = 50;
[MaxLength(MaxLength)]
public virtual string Dist { get; set; }
public virtual ICollection<TaxMapLot> TaxMapLots { get; set; }
}
public async Task<int?> EditPropertyAsync(CreateOrEditPropertyInput input)
{
var property = await _propertyRepository.FirstOrDefaultAsync(p => p.Id == input.Property.Id);
input.Property.MapTo(property);
await _propertyRepository.UpdateAsync(property);//issue is here
return input.Property.Id;
}
public class CreateOrEditPropertyInput : IInputDto
{
[Required]
public PropertyEditDto Property { get; set; }
}
[AutoMap(typeof(Property))]
public class PropertyEditDto
{
public const int MaxLength = 50;
public int? Id { get; set; }
[MaxLength(MaxLength)]
public string Dist { get; set; }
public List<TaxMapLotDto> TaxMapLots { get; set; }
}
As I mentioned above Where I have used repositories on my app.So could you tell me how to sort out above issue ? 正如我上面提到的,我在我的应用程序中使用存储库的位置,那么你能告诉我如何解决以上问题吗? Thanks.
谢谢。
Note : I can add records to the db.But problem occurs when I try to do the update. 注意:我可以将记录添加到db.But尝试执行更新时会出现问题。
Image representation : 图像表示:
1 : M 1:M
This comes when you click the Lot Info
Button above. 当您点击上方的“
Lot Info
按钮时,就会出现这种情况。
Note : There are 2 tables.One is Properties
and other is TaxMapLots
.The relationship is 1 : M
. 注意:有2个表,一个是
Properties
,另一个是TaxMapLots
关系是1 : M
。 User can add/edit or delete
records on the TaxMapLot form
. 用户可以在
TaxMapLot form
上add/edit or delete
记录。
Update : Actually I can Add a record.The problem occurs when I try to edit the record. 更新:实际上我可以添加一条记录。尝试编辑记录时出现问题。
This works fine ( CreatePropertyAsync
): It adds record to both the table (Properties and TaxMapLots).The problem is on the Edit method as shown above. 可以正常工作(
CreatePropertyAsync
):将记录添加到表(Properties和TaxMapLots)中。问题出在如上所示的Edit方法上。
public async Task<int> CreatePropertyAsync(CreateOrEditPropertyInput input)
{
var property = input.Property.MapTo<Property>();
var propertyId = await _propertyRepository.InsertAndGetIdAsync(property);
return propertyId;
}
This happens because previous related TaxMapLot are being removed from the TaxMapLots collection when you are mapping dto to entity, as your dto contains full graph. 发生这种情况的原因是,当您将dto映射到实体时,由于dto包含完整图形,因此先前的相关TaxMapLot已从TaxMapLots集合中删除。
EF thinks your collection items are new ones and previous are being removed so theirs PropertyIds becomes null. EF认为您的收藏品是新收藏品,而先前的收藏品已被删除,因此它们的PropertyIds为空。 You need to handle updating TaxMapLot by yourself instead of letting mapper do it for you.
您需要自己处理更新TaxMapLot,而不是让mapper为您完成。
Make foreign key PropertyId nullable in both, datatabase table IpTaxMapLots and TaxMapLot entity: 使数据库表IpTaxMapLots和TaxMapLot实体中的外键PropertyId都为空:
public virtual int? PropertyId { get; set; }
Of course you need to think if for your business make sense to have a TaxMapLot without a Property. 当然,您需要考虑对于您的业务来说有意义的是没有属性的TaxMapLot。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.