Exception Collection Exception for SQL CE 4.0

I am first using EF 4.1 code 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 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 the same time, in the code, I need to update the entire ContactPerson collection of the client.

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 objects that do not display the property foreign key for their relationships. The EntityEntries property will return null because a single object cannot be identified as the source of the exception. Exception handling during saving can be performed easier by exposing the foreign key properties in its object types. See InnerException for details.

with InnerException:

"Customer_ContactPersons" "". , "Customer_ContactPersons_Target" "".

, , . ?

+5
1

:

customer.ContactPersons = modifiedContactPersons;

:

  • Deleted
  • Added

- , , ContactPerson, - Customer, (FK ).

, . , , . , . , , . ? :

  • , = , .
  • , ContractPerson, . , ContractPerson Id -, , .
  • , , . , , .
+8

All Articles