Entity Framework – Inserting into multiple tables using foreign key

前端 未结 1 2006
忘了有多久
忘了有多久 2020-12-28 10:55

I am a relative newby to EF and have created a simple model (see below) using EF4. \"alt

I am having a p

1条回答
  •  别那么骄傲
    2020-12-28 11:27

    Your UserDetail object should have a property that links back to the UserRecord object. (It is maybe called User? If I'm reading the navigation section below correctly.) If you set that property to your new, uncommitted UserRecord object, it will automatically fill in the key when it commits both objects.

    UserRecord userRecord = new UserRecord();
    // whatever
    
    UserDetail userDetail = new UserDetail();
    userDetail.User = userRecord; // This will auto-fill the FK during commit.
    // whatever
    
    // Add both userRecord and userDetail to the context and commit.
    

    0 讨论(0)
提交回复
热议问题