首页 > 解决方案 > 如何在 ASP.NET MVC 中更新相关数据?

问题描述

我在 ASP.NET MVC 中有两个具有多对多关系的表,如下所示:

public class Course
{
        [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public int Capacity { get; set; }

        public virtual ICollection<DayTime> DayTimes{ set; get; }
}

public class DayTime
{
       public int Id { get; set; }
       public WeekDaysEn Day { get; set; }
       public string StartTime { get; set; }
       public string EndTime { get; set; }
       public virtual ICollection<Course> Course{ get; set; }
}

当记录Course是这样的:

{
    id = 1,
    title = 'test',
    capacity = 20, 
    { 
        DayTimes = { id = 2, Day = 0, StartTime = '10:00', endTime = '12:00' }
    }
}

当我尝试更新它并定义如下数据时:

{id=1,title='test',capacity=20, 
                  {DayTimes={id=2,Day=0,StartTime='10:00',endTime='12:00'},
                            {id=2,Day=0,StartTime='12:00',endTime='14:00'}
}

DayTime记录未在CourseDayTime表中更新

标签: asp.net-mvcentity-framework

解决方案


推荐阅读