I am using Entity Framework 6.1.3 and have a scenario in which I get an object with its navigation property (using Include ()) and disconnects it from the context, changes the foreign key identifier, and then attaches it to the new DbContext again:
// Init the Db using (var db = new MyContext()) { var theWarranty = new ProductWarranty { WarrantyName = "The Warranty" }; var newWarranty = new ProductWarranty { WarrantyName = "New Warranty" }; var brand = new ProductBrand { BrandName = "The Brand", DefaultWarranty = theWarranty }; db.ProductBrands.Add(brand); db.ProductWarranties.Add(newWarranty); db.SaveChanges(); } // Load the detached Brand ProductBrand detachedBrand; using (var db = new MyContext()) { detachedBrand = db.ProductBrands.AsNoTracking() .Include(b => b.DefaultWarranty) // <<< If this line is removed the Attach works .First(x => x.Id == 1); } // Modify the Default Warranty Foreign Key detachedBrand.DefaultWarranty = null; detachedBrand.DefaultWarranty_Id = 2; // Attempt to re-attach and save the changes using (var db = new MyContext()) { var entity = db.Set<ProductBrand>().Attach(detachedBrand); // <<< This line throws the exception db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); }
I get:
A violation of the referential integrity constraint has occurred. Property values> ProductLarranty.Id at one end of the relationship do not match property> ProductBrand.DefaultWarranty_Id 'at the other end.
However, if I DO NOT use Include (), the application works fine.
I really need the navigation property (DefaultWarranty) in the real scenario, but I donโt see the difference in enabling navigation in a separate object, and not in loading it into a separate object. In my experience and reading, this should be the case if the foreign key is set to a new value and the navigation property is null.
I read the Ladislav blog on Foreign Key vs Independent properties http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/ , but it does not quite cope with this scenario and from which I can say that I use foreign keys in this case.
What happens and what is the correct way to change foreign keys with navigation properties enabled, such as this scenario?
It almost looks like EF didnโt โcompletelyโ detach the object when Include ... is used, which also seems strange.
Here is a simplified setup:
Product brand
public partial class ProductBrand { public int Id { get; set; } public string BrandName { get; set; } public Nullable<int> DefaultWarranty_Id { get; set; } public virtual ProductWarranty DefaultWarranty { get; set; } }
Product Token Map
public class ProductBrandMap : EntityTypeConfiguration<ProductBrand> { public ProductBrandMap() {
Product Warranty
public partial class ProductWarranty { public ProductWarranty() { this.ProductBrands = new List<ProductBrand>(); } public int Id { get; set; } public string WarrantyName { get; set; } public virtual ICollection<ProductBrand> ProductBrands { get; set; } }
Product Warranty Card
public class ProductWarrantyMap : EntityTypeConfiguration<ProductWarranty> { public ProductWarrantyMap() {