我有两个实体(student
,yogaspaceevent
)。 Yogaspaceevent
就像一个练习课,有一群学生为RegisteredStudents
,我希望能够将学生添加到此活动中进行注册。
我尝试过几件事,但似乎无法将一个学生添加到yogaspaceevent,所以也许我做得不对。
问题:我是否需要创建一个全新的实体(比如registeredstudents
)才能将学生添加到yogaspaceevent
?因为我认为Entity Framework会以某种方式为我创建这个表,或者在引擎盖上做一些其他魔术来为学生添加活动?我唯一的解决方案是创建一个RegisteredStudent
实体,然后EF将创建一个表。这是正确的解决方案吗?
以下是我的两个实体:
public class YogaSpaceEvent
{
public int YogaSpaceEventId { get; set; }
[Index]
public DateTime DateTimeScheduled { get; set; }
public int AppointmentLength { get; set; }
public int StatusEnum { get; set; }
public virtual ICollection<Student.Student> RegisteredStudents { get; set; }
[Index]
public int YogaSpaceRefId { get; set; }
[ForeignKey("YogaSpaceRefId")]
public virtual YogaSpace YogaSpace { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public virtual StudentImage StudentImage { get; set; } //one-to-one
[MaxLength(50)]
public string CatchPhrase { get; set; }
[MaxLength(250)]
public string Education { get; set; } //Serra High School, San Diego State Univesity
public string Work { get; set; } // Taco Bell, Starbucks
[MaxLength(250)]
public string WhyIPractice { get; set; }
public StudentStatus Status { get; set; }
}
这是我的控制器,我想在一个活动中添加一个学生,但我会抛出异常。
[HttpPost]
public ActionResult RegisterStudentForEvent(int eventId)
{
try
{
Student student = _studentRepository.Find(User.Identity.GetUserId()); //User.Identity.GetUserId()
if (student == null)
{
return Json( new { error = "notification", message = "You need an active student profile to register, please complete your student profile." });
}
var spaceEvent = _yogaSpaceEventRepository.Find(eventId);
if (spaceEvent.RegisteredStudents == null)
{
spaceEvent.RegisteredStudents = new Collection<Student>(); // new EntityCollection<Student>(); neither of these two work, both throw exceptions
}
spaceEvent.RegisteredStudents.Add(student);
_yogaSpaceEventRepository.Modify(spaceEvent);
_yogaSpaceEventRepository.Save();
// send email confirmation with a cancel link in it.
return Json(new { error = "false", message = "Now registered!. A confirmation email has been sent. Have fun! " });
}
catch (Exception ex)
{
return Json( new { error = "true", message = ex.Message });
}
}
以下是我尝试上述两种方法时抛出的两个异常。
无法将对象添加到EntityCollection或EntityReference。附加到ObjectContext的对象无法添加到与源对象无关的EntityCollection或EntityReference。
和
无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象。
答案 0 :(得分:0)
我遇到了类似的问题。
您的上述代码似乎是正确的,我认为您错过了在dbcontext.cs类中指定映射导致以下错误:
无法将对象添加到EntityCollection或 的EntityReference。附加到ObjectContext的对象不能 被添加到不是的EntityCollection或EntityReference 与源对象相关联。
我希望我的下面的例子可以帮到你。
[Table("M_Specialization")]
public partial class Specialization : Entity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int SID { get; set; }
public string SCode { get; set; }
public string Description { get; set; }
public virtual ICollection<SubSpecialization> SubSpecializationDetails { get; set; }
}
[Table("M_SubSpecialization")]
public partial class SubSpecialization : Entity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Int32 SuID { get; set; }
public string SubCode { get; set; }
public Int32 SID { get; set; } // This is required in order to map between specialization and subspecialization
[ForeignKey("SID")]
public virtual Specialization Specialization { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SubSpecialization>().HasRequired(c => c.Specialization)
.WithMany(s => s.SubSpecializationDetails)
.HasForeignKey(c => c.SID);
}
答案 1 :(得分:-1)