I have to classes PersonContactInfo and Address. There are two Navigation properties AlternateAddress and .MailingAddress within PersonContactInfo class. In case then i try add AlternateAddress and delete MailingAddress within the same transaction i have following error : DbUpdateException. I can see in Profile that ce tries to delete AlternateAddress previous to update PersonContactInfo, and get ForeignKey violation exception cause PersonContactInfo still has reference to AlternateAddress. In case then i try to only add or only delete one of navigation properties every thing works fine.
Code Example:
[TestMethod]
public void Test()
{
var context = new DbContext("Connection");
var addressRepository = new Repository<Address>(context);
var repository = new Repository<PersonContactInfo>(context);
PersonContactInfo personContactInfo = repository.Get(Guid.Parse("33E11517-0E8B-E211-97BF-005056C00008"))));
if (personContactInfo != null)
{
personContactInfo.AlternateAddress = new Address();
addressRepository.Delete(personContactInfo.MailingAddress);
repository.InsertOrUpdate(personContactInfo);
context.SaveChanges();
}
}
Could you please recomend solution for the problem or workaround.
Code Example:
[TestMethod]
public void Test()
{
var context = new DbContext("Connection");
var addressRepository = new Repository<Address>(context);
var repository = new Repository<PersonContactInfo>(context);
PersonContactInfo personContactInfo = repository.Get(Guid.Parse("33E11517-0E8B-E211-97BF-005056C00008"))));
if (personContactInfo != null)
{
personContactInfo.AlternateAddress = new Address();
addressRepository.Delete(personContactInfo.MailingAddress);
repository.InsertOrUpdate(personContactInfo);
context.SaveChanges();
}
}
Could you please recomend solution for the problem or workaround.