Winforms 使用EF6和WinForm在多对多关系中插入新记录
在我的例子中,我有一个Winforms 使用EF6和WinForm在多对多关系中插入新记录,winforms,entity-framework,insert-update,Winforms,Entity Framework,Insert Update,在我的例子中,我有一个WinForm项目和一个服务器(使用EF)。有两个实体(医生和诊所)具有多对多关系,如: public class Doctor { public Int64 ID { get; set; } public virtual ICollection<Clinic> clinics { get; set; } // And some others } public class Clinic { public In
WinForm
项目和一个服务器(使用EF)。有两个实体(医生
和诊所
)具有多对多关系,如:
public class Doctor
{
public Int64 ID { get; set; }
public virtual ICollection<Clinic> clinics { get; set; }
// And some others
}
public class Clinic
{
public Int64 ID { get; set; }
public virtual ICollection<Doctor> doctors { get; set; }
//And some others
}
这是我发送到数据库以更新医生实体的信息:
Doctor_ID : 1
Clinics : {Clinic_ID : 1 , Clinic_ID : 2 }
这是我在服务器中的更新方法:
[HttpPut]
[ResponseType(typeof(Doctor))]
// PUT: api/Doctors/registerNewClinicForExistingDoctor
public IHttpActionResult registerNewClinicForExistingDoctor(Doctor doctor)
{
db.doctors.Attach(doctor);
db.Entry(doctor).Property(x => x.clinics).IsModified = true;
db.SaveChanges();
return Ok(doctor);
}
当我试图更新Doctor
实体时,发生了此错误
类型“Doctor”上的属性“clinics”不是原始或复杂的属性。Property方法只能用于基本属性或复杂属性。使用引用或收集方法
在这张图片中,您可以在更新之前看到Doctor类(在winform中)
现在,请告诉我,当我想为现有医生注册新诊所时,我必须做什么。怎么了
[HttpPut]
[ResponseType(typeof(Doctor))]
// PUT: api/Doctors/registerNewClinicForExistingDoctor
public IHttpActionResult registerNewClinicForExistingDoctor(Doctor doctor)
{
db.doctors.Attach(doctor);
db.Entry(doctor).Property(x => x.clinics).IsModified = true;
db.SaveChanges();
return Ok(doctor);
}