I have 2 tables Jobs and Schedule
Jobs
JobId – int,PK, Identity
ScheduleId – int, FK
Title – varchar
Description – varchar
Schedules
ScheduleId – int,PK, Identity
Name – varchar
There is a relationship one to many with cascade on delete.
When I create Entity model, the generated Jobs model removes the ScheduleId field.
The Problem is that I can't Insert new Job with specified ScheduleId!
Job job = new Job();
job.title= "blabla";
job.description="xyz";
job.scheduleId=1// can't have this!
if (job.EntityState == EntityState.Detached)
{
myContext.AddToJobs(job);
}
myContext.SaveChanges();
Note: I have a row in Schedules table with scheduleId=1.
Best Answer
Doesn't the entity framework created a navigation property called Schedule?
You can use:
The Id assignment is handled internally by the framework.
Here is a sample of a Job and a Schedule class created by Entity Framework with the POCO T4. Including the navigation property and the ScheduleId. Notice that the ScheduleId and Schedule Proprerties in the Job class are totaly binded one to the other.