开发者

LINQ to SQL: table insert on multiple tables

I have two tables, RESTAURANT and HOURS with REST_ID as the key between the two tables. i receive an error when I get to the first line of code to add HOURs. The error asks开发者_如何学运维 to create an instance of the object but Intellisence allows me to call that table reference. Here is a snipped of the code:

RESTAURANT addRest = new RESTAURANT();
    addRest.REST_NAME = r_name;
    addRest.REST_STREET1 = r_street;
    addRest.REST_PHONE = r_phone;
    addRest.REST_WEBSITE = r_web;
    addRest.REST_DESC = r_desc;
    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();


HOUR is a contained object inside of RESTAURANT. You need to instantiate it before setting properties on it (like a typical C# object):

addRest.HOUR = new HOUR();
addRest.HOUR.HOURS_SUN = h_su;
...


You haven't created an HOUR object on your RESTAURANT, so that navigation property is null.

...
addRest.REST_DESC = r_desc;
addRest.HOUR = new HOUR();
addRest.HOUR.HOURS_SUN = h_su;
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜