在迁移期间将ICollection <t>传递给数据库

时间:2016-09-26 17:34:03

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

您好我正试图了解MVC,并且我试图将使用angular创建的本地网站迁移到.NET MVC应用程序。从我用于网站的json文件中我试图为MVC应用程序创建数据库模型,但我仍然坚持这个错误:

  

无法创建类型的常量值   &#39; ConFusionMVC.Models.Comment&#39 ;.只有原始类型或枚举   在这种情况下支持类型。

这是示例json:

{
      "id": 1,
      "name": "Zucchipakoda",
      "image": "images/zucchipakoda.png",
      "category": "appetizer",
      "label": "",
      "price": 1.99,
      "description": "dish description",
      "comments": [
        {
          "rating": 5,
          "comment": "Imagine all the eatables, living in conFusion!",
          "author": "John Lemon",
          "date": "2012-10-16T17:57:28.556094Z"
        },
        {
          "rating": 4,
          "comment": "Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
          "author": "Paul McVites",
          "date": "2014-09-05T17:57:28.556094Z"
        },
        {
          "rating": 3,
          "comment": "Eat it, just eat it!",
          "author": "Michael Jaikishan",
          "date": "2015-02-13T17:57:28.556094Z"
        },
      ]
    }

所以一般的想法是拥有2个模型&#39; Dish&#39;和#39;评论&#39;与1:很多关系,因此Dish包含ICollection of Comments。论文是

public class Dish
    {
        [Key]
        public int DishID { get; set; }
        public string Name { get; set; }
        public string Image { get; set; }
        public string Category { get; set; }
        public string Label { get; set; }
        public double Price { get; set; }
        public string Description { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

public class Comment
    {
        [Key]
        public int CommentID { get; set; }
        public int Rating { get; set; }
        public string Comments { get; set; }
        public string Author { get; set; }
        public DateTime DatePosted { get; set; }
    }

我的DbContext类:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Dish> Dishes { get; set; }
        public DbSet<Comment> Comments { get; set; }


        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Dish>()
                .HasMany(s => s.Comments);

            base.OnModelCreating(modelBuilder);

        }

我正在使用代码优先迁移方法,这是我遇到错误的地方。这是我的种子实现:

var comments = new List<Comment>
            {
                new Comment { Rating = 5, Author = "John Lemon",
                    Comments = "author comments",
                    DatePosted = DateTime.Parse("2012-10-16T17:57:28.556094Z")
                },
                new Comment { Rating = 4, Author = "Paul McVites",
                    Comments = "author comments",
                    DatePosted = DateTime.Parse("2014-09-05T17:57:28.556094Z")
                },
                new Comment { Rating = 3, Author = "Michael Jaikishan",
                    Comments = "Eat it, just eat it!", DatePosted = DateTime.Parse("2015-02-13T17:57:28.556094Z")
                },

            };

            comments.ForEach(p => context.Comments.AddOrUpdate(c => c.CommentID, p));
            context.SaveChanges();

            var dishes = new List<Dish>
            {

                new Dish
                {
                    Name ="Uthapizza", Image="~/Images/uthapizza.png", Category="Mains",
                    Label ="Hot", Price=4.99, Description="dish comments" ,
                    Comments = new List<Comment>()
                    {
                        comments[0],comments[1], comments[2]
                    }
                },
                new Dish
                {
                    Name="Zucchipakoda", Image="~/Images/zucchipakoda.png",
                    Category="Appetizer", Label="Hot", Price=1.99, Description = "dish comments",
                    Comments = new List<Comment>()
                    {
                        comments[0],comments[1], comments[2]
                    }
                },

            };

            dishes.ForEach(s => context.Dishes.AddOrUpdate(p => p.Comments, s));
            context.SaveChanges();
        }

如何将每条菜肴的评论保存到数据库中?请协助

1 个答案:

答案 0 :(得分:1)

您可以尝试如下所示。

var idsAndFullNames = idsAndFullNamesFromOneSource
    .AsEnumerable()        // force LINQ-to-Objects to take over
    .Select(src => new 
    {
        id = src.ID,
        fullName = src.FullName
    })
    .Union(
        idsAndFullNamesFromOtherSource.Select(src => new
        {
           id = src.Id,
           fullName = src.full_name
        })
    .ToList();