我在實體框架中有多對多的關係。請查看圖片...
我想插入此項目的項目和標籤,但 TagName屬性是分貝唯一的,因此我需要在Tags
插入並在ArticlesToTags
只有新標籤(不可見在EF)表中,而其他已經在Tags
中的其他字段只需要插入ArticlesToTags
。EF中插入多對多邏輯
public void CreateUpdate(string title, string subTitle, string text,
string author, string tags, string photo, bool allowComments)
{
using (var context = new blogEntities())
{
var article = new Article()
{
Title = title,
SubTitle = subTitle,
ArticleText = text,
Author = author,
Photo = photo,
CreateDate = DateTime.Now,
ModifyDate = null,
AllowComments = allowComments
};
foreach (var tg in tags.Split(','))
{
article.Tags.Add(new Tag() { TagName = tg });
}
context.Articles.AddObject(article);
context.SaveChanges();
}
}
現在拋出重複的唯一鍵的例外不能插入它是如何在EF做了什麼?我是新手...