EntityFramework - 更新多对多关系

时间:2016-10-27 14:50:08

标签: c# asp.net-mvc entity-framework

我试图更新多对多关系。我有两个模型,日记和标签​​。模型日记包含标签列表,标签包含日记列表。但每当我尝试将另一个标记添加到现有列表或更改现有列表时,我都会抛出异常:

实体类型List`1不是当前上下文模型的一部分。

我的更新方式是否适用于集合?或者我应该考虑另一种方法吗?

日记模型

public class Diary
{
    [Key]
    public int IdDiary { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public string NameDiary { get; set; }

    [Required(ErrorMessage = "This field is required")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime CreationDate { get; set; }

    public bool Locked { get; set; }

    public ICollection<Entry> Entries { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "This field is required")]
    public string Summary { get; set; }

    public string ImageUrl { get; set; }

    public ICollection<Tag> Tags { get; set; }
}

条目模型

public class Entry
{
    [Key]
    public int IdEntry { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime CreationDate { get; set; }

    [DataType(DataType.MultilineText)]
    public string EntryText { get; set; }

    [ForeignKey("Diary_IdDiary")]
    public Diary Diary { get; set; }

    public int Diary_IdDiary { get; set; }
    public string EntryName { get; set; }
}

标记模型

public class Tag
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Diary> Diaries { get; set; }
}

更新方法

    public void UpdateDiary(Diary updatedDiary)
    {
        var searchResult = SearchDiary(updatedDiary);
        if (searchResult != null)
        {
            updatedDiary.IdDiary = searchResult.IdDiary;
            _context.Entry(searchResult).CurrentValues.SetValues(updatedDiary);
            _context.Entry(searchResult.Tags).CurrentValues.SetValues(updatedDiary.Tags);
            _context.SaveChanges();
        }
    }

SearchDiary方法

    public Diary SearchDiary(Diary searchDiary)
    {
        var queryResult =
            _context.Diaries.Include(d => d.Entries).Include(d => d.Tags)
                .Where(d => (d.NameDiary == searchDiary.NameDiary && d.CreationDate == searchDiary.CreationDate) || d.IdDiary == searchDiary.IdDiary);
        return queryResult.FirstOrDefault();
    }

感谢您阅读

1 个答案:

答案 0 :(得分:0)

这是一种方式:

<强> DiaryModel

public class Diary
{
    [Key]
    public int IdDiary { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public string NameDiary { get; set; }

    [Required(ErrorMessage = "This field is required")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime CreationDate { get; set; }

    public bool Locked { get; set; }

    public virtual ICollection<Entry> Entries { get; set; }

    [DataType(DataType.MultilineText)]
    [Required(ErrorMessage = "This field is required")]
    public string Summary { get; set; }

    public string ImageUrl { get; set; }

    public ICollection<Tag> Tags { get; set; }
}

<强> EntryModel

public class Entry
{
    [Key]
    public int IdEntry { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime CreationDate { get; set; }

    [DataType(DataType.MultilineText)]
    public string EntryText { get; set; }

    [ForeignKey("Diary_IdDiary")]
    public virtual Diary Diary { get; set; }

    public int Diary_IdDiary { get; set; }
    public string EntryName { get; set; }
}

搜索

public Diary SearchDiary(int DiaryId)
{
    return _context.Diaries
        .Include(p => p.Entries)
        .Include(p => p.Tags)
        .FirstOrDefault(p => p.IdDiary == DiaryId);
}

更新

public void UpdateDiary(Diary Diary)
{
    _context.Update(Diary);
    _context.SaveChanges();
}

您应该将修改后的Diary对象传递给更新函数。