I'm using EF 4.1 code first with SQL ce 4.0
I have two classes
public class Customer
{
public int ID { get; set; }
public string CompanyName { get; set; }
public List<ContactPerson> ContactPersons { get; set; }
}
public class ContactPerson
{
public int ID { get; set; }
public string Name { get; set; }
}
and a DbContext
public class MyDB : DbContext
{
public DbSet<ContactPerson> ContactPersons { get; set; }
public DbSet<Customer> Customers { get; set; }
public MyDB()
{
this.Configuration.LazyLoadingEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasMany(c => c.ContactPersons)
.WithRequired()
.WillCascadeOnDelete(true);
}
}
At same time in the code I need to update the whole collection of ContactPerson of a Customer.
List<ContactPersons> modifiedContactPersons;
Customer customer = MyDB.Customers.Find(ID);
customer.ContactPersons = modifiedContactPersons;
when I call MyDB.SaveChanges() I get the following exception:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.
with InnerException:
A relationship from the 'Customer_ContactPersons' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Customer_ContactPersons_Target' must also in the 'Deleted' state.
I understand what this does imply but I cannot solve the problem by myself. Any suggestions?