无法更改关系,因为一个或多个外键属性不可为空 [英] The relationship could not be changed because one or more of the foreign-key properties is non-nullable
问题描述
例外:
System.InvalidOperationException:操作失败:关系不能
被更改,因为一个或多个外键属性不可为空。当
更改关系时,相关的外键属性设置为
null值。如果外键不支持空值,则必须定义新关系
,外键属性必须分配另一个非空值
或不相关对象必须删除。
我已经看到上述问题的一些解决方案,如下所示。但是他们都没有为我工作: (可能是由于这些解决方案是为EF 4.x版本。我的应用程序的EF版本是6.x。
当您点击批次信息
上面的按钮。
注意:有2个表。一个是属性
而其他是 TaxMapLots
。关系是 1:M
。用户可以在 TaxMapLot表单
上添加/编辑或删除记录。
更新:实际上我可以添加记录。当我尝试编辑记录时,会出现问题。
CreatePropertyAsync
):它将记录添加到表(属性和TaxMapLots)。问题出在如上所示的编辑方法。
public async Task< int> CreatePropertyAsync(CreateOrEditPropertyInput input)
{
var property = input.Property.MapTo< Property>();
var propertyId = await _propertyRepository.InsertAndGetIdAsync(property);
return propertyId;
}
使外键PropertyId为空两者,数据库表IpTaxMapLots和TaxMapLot实体:
public virtual int? PropertyId {get;组; }
当然,您需要考虑如果您的业务有一个TaxMapLot没有属性。
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.
Solution 1 and Solution 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.
Image representation :
1 : M
This comes when you click the Lot Info
Button above.
Note : There are 2 tables.One is Properties
and other is TaxMapLots
.The relationship is 1 : M
. User can add/edit or delete
records on the TaxMapLot form
.
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.
public async Task<int> CreatePropertyAsync(CreateOrEditPropertyInput input)
{
var property = input.Property.MapTo<Property>();
var propertyId = await _propertyRepository.InsertAndGetIdAsync(property);
return propertyId;
}
Make foreign key PropertyId nullable in both, datatabase table IpTaxMapLots and TaxMapLot entity:
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.
这篇关于无法更改关系,因为一个或多个外键属性不可为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!