开发者

mvc3 with EF code first insert many to many

I have a Many to Many relationship defined as

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; }
}

i can easily insert an Article and a tag but when I try to insert

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();

i get

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 

How can I insert the Tags for an article?

Thanks


You seem to be creating tags like this:

Tags = new List<Tag>
{
    new Tag{Id = 1}, 
    new Tag{Id = 2}
}

Giving only the Id. But in your model you have the Name property also specified as [Required], see:

[Required]
public string Name { get; set; }

To fix, try including names for the tag. e.g.:

Tags = new List<Tag>
{
    new Tag{Id = 1, Name = "some name"}, 
    new Tag{Id = 2, Name = "another tag name"}
}

Alternatively, remove the [Required] attribute from the Name property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜