none
EF and multiple 1:1 table references

    Question

  • I am trying to map 2 simple EF entities (Customer and Address) that simple relationship. A Customer must have a ShipTo and a BillTo addess. I set up 2x 1:1 relationships between customer and address using endpoints only on customer side. When I try to add a customer and his 2 addresses I get the following error:

    Entities in 'Model1Container1.Addresses' participate in the 'CustomerAddress1' relationship. 0 related 'Customer' were found. 1 'Customer' is expected.

    If I take away one of the associations, it seems to work. Also, this is a "Generate from Model" setup. The code is below.

    Thanks for any light you can shed...

    using (var context = new Model1Container1())
                {
                    context.ExecuteStoreCommand("delete from Addresses");
                    context.ExecuteStoreCommand("delete from Customers");
                    context.SaveChanges();

                    var addr1 = new Address { Zip = "10001" };
                    var addr2 = new Address { Zip = "10002" };
                    var cust = new Customer()
                    {
                        Name = "Me",
                        BillTo = addr1,
                        ShipTo = addr2,
                    };

                    context.AddToCustomers(cust);

                    context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                }
    • Moved by SamAgain Friday, September 17, 2010 8:39 AM better fit (From:.NET Base Class Library)
    Friday, September 17, 2010 4:25 AM

All replies