I have what I consider to be a very simple data model and I am struggling poorly with EF 4.1 CF.
My data model has two classes:
public class Site { public int id { get; set; } public string name { get; set; } public ICollection<Building> buildings { get; set; } } public class Building { public int id { get; set; } public int siteId { get; set; } public string name { get; set; } }
My configuration files are as follows:
public class SiteConfiguration : EntityTypeConfiguration<Site> { public SiteConfiguration() { HasMany(c => c.buildings) .WithRequired() .HasForeignKey(c => c.siteId); } }
In my controller for MVC, I just want to remove the building from the site. Here is my controller code:
public ActionResult Delete(int id, int siteId) { var site = repo.GetById(siteId); var building = site.buildings.SingleOrDefault(c => c.id == id); ou.buildings.Remove(site); repo.Save(); }
My error message:
The operation failed: the relation cannot be changed because one or more properties of the foreign key are nonzero. When a relationship change occurs, the related foreign-key property is null. If the foreign key does not support null values, a new relationship must be defined, a foreign key property must be assigned another non-zero value, or an unrelated object must be deleted. Any thoughts or suggestions are greatly appreciated.
source share