Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Commented Issue: Add and Delete different navigation properties of the same type [949]

$
0
0
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.
Comments: Hi, I was unable to reproduce this issue in my environment. Which build of EntityFramework are you using? You are using SQL CE, correct? If you are still seeing the issue, could you provide more details regarding the implementation you used? (implementation of repository, entities and mapping setup, if any) I created the following repro, based on the information you provided, but implementation details might be different and that's what is causing the failure in your case. You can try to use the implementation below as a workaround. I verified that it works against latest EF/EF.CE bits. namespace Investigate949 { using System; using System.Data.Entity; using System.Data.Entity.Migrations; public interface IRepository<TEntity> { TEntity Get(Guid id); void Delete(TEntity entity); void InsertOrUpdate(TEntity entity); } public class MyRepository<TEntity> : IRepository<TEntity> where TEntity : class { private MyContext _context; public MyRepository(MyContext context) { _context = context; } public TEntity Get(Guid id) { return _context.Set<TEntity>().Find(id); } public void Delete(TEntity entity) { _context.Set<TEntity>().Remove(entity); } public void InsertOrUpdate(TEntity entity) { _context.Set<TEntity>().AddOrUpdate(entity); } } public class Address { public int Id { get; set; } public string Street { get; set; } public string Zip { get; set; } } public class PersonContactInfo { public Guid Id { get; set; } public string Name { get; set; } public virtual Address AlternateAddress { get; set; } public virtual Address MailingAddress { get; set; } } public class MyContext : DbContext { public DbSet<Address> Addresses { get; set; } public DbSet<PersonContactInfo> ContactInfos { get; set; } } public class MyInitializer : DropCreateDatabaseAlways<MyContext> { protected override void Seed(MyContext context) { var address1 = new Address { Street = "Street1", Zip = "Zip1", }; var address2 = new Address { Street = "Street2", Zip = "Zip2", }; var address3 = new Address { Street = "Street3", Zip = "Zip3", }; var address4 = new Address { Street = "Street4", Zip = "Zip4", }; context.Addresses.Add(address1); context.Addresses.Add(address2); context.Addresses.Add(address3); context.Addresses.Add(address4); var contactInfo1 = new PersonContactInfo { Id = Guid.Parse("33E11517-0E8B-E211-97BF-005056C00008"), Name = "Name1", AlternateAddress = address1, MailingAddress = address2, }; var contactInfo2 = new PersonContactInfo { Id = Guid.Parse("33E11517-0E8B-E211-97BF-005056C00009"), Name = "Name2", AlternateAddress = address3, MailingAddress = address4, }; context.ContactInfos.Add(contactInfo1); context.ContactInfos.Add(contactInfo2); } } class Program { static void Main(string[] args) { Database.SetInitializer(new MyInitializer()); using (var context = new MyContext()) { var cusomerRepository = new MyRepository<PersonContactInfo>(context); var addressRepository = new MyRepository<Address>(context); var customer = cusomerRepository.Get(Guid.Parse("33E11517-0E8B-E211-97BF-005056C00008")); customer.AlternateAddress = new Address(); addressRepository.Delete(customer.MailingAddress); cusomerRepository.InsertOrUpdate(customer); context.SaveChanges(); } } } }

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>