我将多对多关系定义为
public class Article
{
[Required]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Content { get; set; }
[Required]
public bool IsPublished { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public int UserId { get; set; }
[Required]
public double Rating { get; set; }
[Required]
public int RatingCount { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Tag>Tags { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Tag
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
我可以轻松插入文章和标签但是当我尝试插入
时var articles = new List<Article>
{
new Article{ Id = 1,
Date = DateTime.Now,
Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu molestie eros. Donec sed orci gravida sem ultricies facilisis in ut diam. Quisque at nulla convallis augue congue vehicula quis vel ligula. Ut ultricies massa sit amet sapien commodo eu luctus arcu malesuada. Pellentesque id augue a augue consectetur mattis. Donec id arcu non est suscipit auctor. Donec quis nulla nisl, ut malesuada quam. Donec commodo rh",
Content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu molestie eros. Donec sed orci gravida sem ultricies facilisis in ut diam. Quisque at nulla convallis augue congue vehicula quis vel ligula. Ut ultricies massa sit amet sapien commodo eu luctus arcu malesuada. Pellentesque id augue a augue consectetur mattis. Donec id arcu non est suscipit auctor. Donec quis nulla nisl, ut malesuada quam. Donec commodo rhoncus aliquam. Phasellus suscipit, sem a euismod bibendum, ipsum leo congue dui, eu sollicitudin eros augue ac dui. Suspendisse non tellus mauris, nec vulputate magna. Morbi ac sem felis. Duis facilisis nisl id sapien aliquam sit amet dignissim tellus pretium. Quisque dapibus aliquam sem, sit amet elementum justo fringilla nec. Integer ac arcu sit amet nulla porttitor volutpat. ",
IsPublished = true,
Title = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
UserId = 1,
Rating = 0,
RatingCount = 0,
Tags = new List<Tag>
{
new Tag{Id = 1},
new Tag{Id = 2}
}
}
};
articles.ForEach(s => dataContext.Article.Add(s));
dataContext.SaveChanges();
我得到了
System.Data.Entity.Validation.DbEntityValidationException : Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at GigaWebSolutionMVC3Tests.DataBaseHelper.AddArticles(GigaWebSolutionMvc3DataContext dataContext) in DataBaseHelper.cs: line 81 at GigaWebSolutionMVC3Tests.DataBaseHelper.AddData(GigaWebSolutionMvc3DataContext dataContext) in DataBaseHelper.cs: line 17 at GigaWebSolutionMVC3Tests.GigaWebSolutionMvc3DataContextTests.Should_be_able_to_add_data_to_the_database() in GigaWebSolutionMVC3DataContextTests.cs: line 41
如何插入文章的标签?
由于
答案 0 :(得分:0)
您似乎正在创建这样的标签:
Tags = new List<Tag>
{
new Tag{Id = 1},
new Tag{Id = 2}
}
仅提供身份证明。但是在您的模型中,您还将Name属性指定为[Required],请参阅:
[Required]
public string Name { get; set; }
要修复,请尝试包含标记的名称。 e.g:
Tags = new List<Tag>
{
new Tag{Id = 1, Name = "some name"},
new Tag{Id = 2, Name = "another tag name"}
}
或者,从Name属性中删除[Required]
属性。