0

I am trying to update 2 tables: RESTAURANT and HOURS. The tables shere the REST_ID key. I get an error on the line with the arrow (==>): Sorry, I'm trying to teach myself this stuff and it's the first time I've tried a multi-table insert.

The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.

   RESTAURANT addRest = new RESTAURANT();
        addRest.REST_NAME = r_name;
        addRest.REST_STREET1 = r_street;
        addRest.CITY_ID = c_id;
        addRest.REST_PHONE = r_phone;
        addRest.REST_WEBSITE = r_web;
        addRest.HOUR = new HOUR();
        addRest.HOUR.HOURS_SUN = h_su;
        addRest.HOUR.HOURS_MON = h_mo;
        addRest.HOUR.HOURS_TUE = h_tu;
        addRest.HOUR.HOURS_WED = h_we;
        addRest.HOUR.HOURS_THU = h_th;
        addRest.HOUR.HOURS_FRI = h_fr;
        addRest.HOUR.HOURS_SAT = h_sa;
        addRest.HOURReference.EntityKey = new EntityKey("FVTCEntities.HOURS", "HOURS", 1);
        ==> db.AddToRESTAURANTs(addRest);
        db.SaveChanges();
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
Susan
  • 1,822
  • 8
  • 47
  • 69

1 Answers1

0

That isn't LINQ to SQL. It's Entity Framework.

You don't need to set the EntityKey, usually. Just set the HOUR properties like any POCO type. Ignore EntityKey unless you have a very specific reason to set it.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • You've got to be kidding ;o)) I've seen all these examples using EntityKey & I've been trying to figure it out for 2 days. Many thanks --I'm humbled! – Susan Aug 05 '11 at 19:43