Foreign conflict

// EDMX file http://pastebin.com/btTCRMf7

I have 2 tables CustomersandSites

//Site
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int CustomerID { get; set; }
public int CityID { get; set; }
public int CountryID { get; set; }
public int EncodedBy { get; set; }
public System.DateTime DateEncoded { get; set; }

public virtual City City { get; set; }
public virtual Country Country { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual User User { get; set; }
public virtual Customer Customer { get; set; }

//Customer
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int CityID { get; set; }
public int CountryID { get; set; }
public int CreditTermID { get; set; }
public int EncodedBy { get; set; }
public System.DateTime DateEncoded { get; set; }
public virtual City City { get; set; }
public virtual Country Country { get; set; }
public virtual CreditTerm CreditTerm { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual ICollection<Site> Sites { get; set; }

//Country
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
public virtual ICollection<Site> Sites { get; set; }

//City
public int ID { get; set; }
public string Name { get; set; }

public virtual ICollection<Customer> Customers { get; set; }
public virtual ICollection<Site> Sites { get; set; }


//SiteModel
private static IQueryable<Site> Build(this DbSet<Site> query)
{
    return query.Include("User").Include("City").Include("Country").Include("Customer");
}

public static Site Find(int siteID)
{
    using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
    {
        Site result = context.Sites.Build().SingleOrDefault(s => s.ID == siteID);
        return result;
    }
}

public static Site Update(Site _updatedSite)
{
    using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
    {
        context.Sites.Attach(_updatedSite);
        context.Entry(_updatedSite).State = EntityState.Modified;
        context.SaveChanges();
        return Find(_updatedSite.ID);
    }
}

Site test = SiteModel.Find(1);
test.City = null;
test.CityID = 1;
test.Country = null;
test.CountryID = 1;

test.Customer = null;
test.CustomerID = 1;

SiteModel.Update(test);

I get A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

however, adding test.Customer.City = null;before updating the object will work. this is similar to Customer.City and Site.City is contrary. can someone explain why? or any workaround?

+4
source share
1 answer

I can explain why. Enable saves the object to load all included objects. So our site object has all the links to your clusses (City, Country, User Client). I think this is a problem. The solution may be to download only the site object:

Site result = context.Sites.SingleOrDefault(s => s.ID == siteID);

. , , , .

, , include, . dbContext

context.SaveChanges();

:

public static Site Update(Site _updatedSite)
  {
     using (DragonRentalsEntities context = new DragonRentalsEntities(new ConfigurationManager().ConnectionString))
     {

          if (context.Entry(_updatedSite).State == EntityState.Detached)
               context.Entry(_updatedSite).State = EntityState.Modified;// attaches  entity and marks it as modified if it is detached

          context.SaveChanges();
          return _updatedSite; //after save changes u have the same object as u send in your Update function
    }
  }

Slauma, null, ,

: , . Btw sql- , db City .

+2

All Articles