The relationship could not be changed because one or more of the foreign-key properties is non-nullable

c# entity-framework-6 repository-pattern

Question

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

img1

This comes when you click the Lot Info Button above.

enter image description here

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;
        }
1
0
5/23/2017 12:13:41 PM

Popular Answer

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.

0
2/13/2016 7:27:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow