Entity Framework: adding an object with a foreign key

I have 2 tables Work and schedule

Job

JobId - int, PK, Identity
ScheduleId - int, FK
Title - varchar
Description - varchar

schedule

ScheduleId - int, PK, Identity
Name - varchar

There is one relationship with the cascade on deletion.

When I create an Entity model, the created Jobs model removes the ScheduleId field.

The problem is that I cannot add a new task with the 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 the schedule table with scheduleId = 1.

+3
source share
2 answers

, Schedule?

:

 Schedule schedule = // Get here the schedule with Id == 1;
 Job job = new Job();
 job.title= "blabla";
 job.description="xyz";
 job.schedule = schedule; //<-- Use the navigation property

 if (job.EntityState == EntityState.Detached)
 {
      myContext.AddToJobs(job);
 }
 myContext.SaveChanges();

.


Job Schedule, Entity Framework POCO T4. ScheduleId. , ScheduleId Schedule Proprerties Job .

public partial class Job
{
    #region Primitive Properties

    public virtual int Id
    {
        get;
        set;
    }

    public virtual int ScheduleId
    {
        get { return _scheduleId; }
        set
        {
            if (_scheduleId != value)
            {
                if (Schedule != null && Schedule.Id != value)
                {
                    Schedule = null;
                }
                _scheduleId = value;
            }
        }
    }
    private int _scheduleId;

    public virtual string Title
    {
        get;
        set;
    }

    public virtual string Description
    {
        get;
        set;
    }

    #endregion
    #region Navigation Properties

    public virtual Schedule Schedule
    {
        get { return _schedule; }
        set
        {
            if (!ReferenceEquals(_schedule, value))
            {
                var previousValue = _schedule;
                _schedule = value;
                FixupSchedule(previousValue);
            }
        }
    }
    private Schedule _schedule;

    #endregion
    #region Association Fixup

    private void FixupSchedule(Schedule previousValue)
    {
        if (previousValue != null && previousValue.Job.Contains(this))
        {
            previousValue.Job.Remove(this);
        }

        if (Schedule != null)
        {
            if (!Schedule.Job.Contains(this))
            {
                Schedule.Job.Add(this);
            }
            if (ScheduleId != Schedule.Id)
            {
                ScheduleId = Schedule.Id;
            }
        }
    }

    #endregion
}

public partial class Schedule
{
    #region Primitive Properties

    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Description
    {
        get;
        set;
    }

    #endregion
    #region Navigation Properties

    public virtual ICollection<Job> Job
    {
        get
        {
            if (_job == null)
            {
                var newCollection = new FixupCollection<Job>();
                newCollection.CollectionChanged += FixupJob;
                _job = newCollection;
            }
            return _job;
        }
        set
        {
            if (!ReferenceEquals(_job, value))
            {
                var previousValue = _job as FixupCollection<Job>;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupJob;
                }
                _job = value;
                var newValue = value as FixupCollection<Job>;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupJob;
                }
            }
        }
    }
    private ICollection<Job> _job;

    #endregion
    #region Association Fixup

    private void FixupJob(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Job item in e.NewItems)
            {
                item.Schedule = this;
            }
        }

        if (e.OldItems != null)
        {
            foreach (Job item in e.OldItems)
            {
                if (ReferenceEquals(item.Schedule, this))
                {
                    item.Schedule = null;
                }
            }
        }
    }

    #endregion
}
+1

Schedule. - :

db = new OneToManyEntities(); 
var address = new Address { Address1 = "Oakumber st", City = "Dallas", State = "Tx", Zip = "76111" }; 
address.CustomerReference.EntityKey = new EntityKey("OneToManyEntities.Customer","CustomerId",2); 
db.AddToAddresses(address);

, :/ - , - .

+1

Source: https://habr.com/ru/post/1759354/


All Articles