[英]Entity Framework: Adding object with foreign key
我有2個工作崗位和時間表
JobId - int,PK,Identity
ScheduleId - int,FK
標題 - varchar
描述 - varchar
ScheduleId - int,PK,Identity
名稱 - varchar
在刪除時級聯有一對多的關系。
當我創建實體模型時,生成的作業模型將刪除ScheduleId字段。
問題是我無法使用指定的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();
注意:我在Scheduleles表中有一行,scheduleId = 1。
實體框架是否創建了名為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();
Id賦值由框架內部處理。
以下是Entity Framework使用POCO T4創建的Job和Schedule類的示例。 包括導航屬性和ScheduleId。 請注意,Job類中的ScheduleId和Schedule Proprerties是完全綁定到另一個。
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
}
您可以在不實際加載Schedule對象的情況下分配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);
我不喜歡這種方法,你必須在字符串中硬編碼實體類型:/如果有人知道如何在沒有這種硬編碼的情況下這樣做 - 請評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.