1
你好,我正在尝试获取MVC的窍门,并试图将一个本地网站迁移到.NET MVC应用程序中。从JSON文件我用的网站,我想创建的MVC应用程序的数据库模型,但我被困在这个错误:在迁移过程中将ICollection <T>传递到数据库
无法创建 类型的恒定值“ConFusionMVC.Models.Comment ”。在此上下文中仅支持基本类型或枚举 类型。
这是样品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型“菜”和“评论”以1:许多关系,使菜含有评论的ICollection的。论文是类
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();
}
如何获得将注释保存到每个单独的菜肴的数据库?请协助
虽然它仍然是相同的错误,但这次它给出了一个错误的位置。它在最后一个addorupdate –
现在的错误是什么?编译时间还是运行时间? – Sampath
这是编译时间。现在它的工作,我正在更新的意见,而不是ID。为什么我试过的方法不行? –