C# 通过ICollection<;T>;在迁移过程中访问数据库
您好,我正在尝试了解MVC的诀窍,我正在尝试将一个使用angular创建的本地网站迁移到一个.NETMVC应用程序。从我用于网站的json文件中,我试图为MVC应用程序创建数据库模型,但我遇到了以下错误: 无法创建类型为的常量值 “ConFusionMVC.Models.Comment”。只有基元类型或枚举 在此上下文中支持类型 这是示例json:C# 通过ICollection<;T>;在迁移过程中访问数据库,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,您好,我正在尝试了解MVC的诀窍,我正在尝试将一个使用angular创建的本地网站迁移到一个.NETMVC应用程序。从我用于网站的json文件中,我试图为MVC应用程序创建数据库模型,但我遇到了以下错误: 无法创建类型为的常量值 “ConFusionMVC.Models.Comment”。只有基元类型或枚举 在此上下文中支持类型 这是示例json: { "id": 1, "name": "Zucchipakoda", "image": "images/zucc
{
"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"
},
]
}
因此,一般的想法是有两个模型“Dish”和“Comments”,它们之间的关系为1:many,因此Dish包含一个i评论集合。这些是课程
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; }
}
公共类菜肴
{
[关键]
public int DishID{get;set;}
公共字符串名称{get;set;}
公共字符串图像{get;set;}
公共字符串类别{get;set;}
公共字符串标签{get;set;}
公共双价{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection注释{get;set;}
}
公开课评论
{
[关键]
public int CommentID{get;set;}
公共整数评级{get;set;}
公共字符串注释{get;set;}
公共字符串作者{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);
}
public类ApplicationDbContext:IdentityDbContext
{
公共数据库集{get;set;}
公共DbSet注释{get;set;}
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.有许多(s=>s.评论);
基于模型创建(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();
}
var注释=新列表
{
新评论{Rating=5,Author=“John Lemon”,
Comments=“author Comments”,
DatePosted=DateTime.Parse(“2012-10-16T17:57:28.556094Z”)
},
新评论{Rating=4,Author=“Paul McVites”,
Comments=“author Comments”,
DatePosted=DateTime.Parse(“2014-09-05T17:57:28.556094Z”)
},
新评论{Rating=3,Author=“Michael Jaikishan”,
Comments=“吃吧,就吃吧!”,DatePosted=DateTime.Parse(“2015-02-13T17:57:28.556094Z”)
},
};
comments.ForEach(p=>context.comments.AddOrUpdate(c=>c.CommentID,p));
SaveChanges();
var=新列表
{
新菜
{
Name=“Uthapizza”,Image=“~/Images/Uthapizza.png”,Category=“Mains”,
Label=“Hot”,Price=4.99,Description=“dish comments”,
注释=新列表()
{
评论[0],评论[1],评论[2]
}
},
新菜
{
Name=“Zucchipakoda”,Image=“~/Images/Zucchipakoda.png”,
Category=“开胃菜”,Label=“热”,Price=1.99,Description=“菜肴评论”,
注释=新列表()
{
评论[0],评论[1],评论[2]
}
},
};
ForEach(s=>context.disks.AddOrUpdate(p=>p.Comments,s));
SaveChanges();
}
如何将每个菜的评论保存到数据库中?请协助您可以按如下所示进行尝试
new Dish
{
Name ="Uthapizza", Image="~/Images/uthapizza.png", Category="Mains",
Label ="Hot", Price=4.99, Description="dish comments" ,
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")
},
}
},
新菜
{
Name=“Uthapizza”,Image=“~/Images/Uthapizza.png”,Category=“Mains”,
Label=“Hot”,Price=4.99,Description=“dish comments”,
注释=新列表()
{
新评论{Rating=5,Author=“John Lemon”,
Comments=“author Comments”,
DatePosted=DateTime.Parse(“2012-10-16T17:57:28.556094Z”)
},
新评论{Rating=4,Author=“Paul McVites”,
Comments=“author Comments”,
DatePosted=DateTime.Parse(“2014-09-05T17:57:28.556094Z”)
},
新评论{Rating=3,Author=“Michael Jaikishan”,
Comments=“吃吧,就吃吧!”,DatePosted=DateTime.Parse(“2015-02-13T17:57:28.556094Z”)
},
}
},
n仍然是相同的错误,但这次它给出了错误位置。它是在最后一个addorUpdateI上,这是编译时。它现在可以工作了,我是通过注释而不是ID来更新的。为什么我尝试的方法不起作用呢?好的。你不能像那样添加列表项
。你必须按照上面的步骤来做。请阅读以下内容:你也可以向上投票
),请查看如何操作。