1
こんにちは私はMVCのハングを取得しようとしていますが、ちょっとした角度で作成されたローカルWebサイトを.NET MVCアプリケーションに移行しようとしています。マイグレーション時にICollection <T>をデータベースに渡す
タイプ の一定の値を作成できません「ConFusionMVC.Models.Comment:私はMVCアプリケーションのためのデータベースモデルを作成しようとしていますが、私は、このエラーにこだわっているウェブサイトで使用JSONファイルから'このコンテキストでは、プリミティブ型または列挙型 のみがサポートされています。
これはサンプルJSONです:料理コメントのICollectionをが含まれているので、多くの関係:
{
"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との「コメント」を持つことです。私は、エラーが生じていますところで、コード最初の移行のアプローチを、使用しています
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 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クラスです。これは私の種子の実装です:
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();
}
個々の皿ごとにデータベースにコメントを保存するにはどうすればよいですか?助けてください
Nahはまだ同じエラーですが、今回はエラーの場所です。それは最後のaddorupdateにあった –
今何がエラーですか?コンパイル時間または実行時間? – Sampath
コンパイル時です。現在、IDの代わりにコメントで更新しています。とにかく私が試みた方法は、とにかく働かなかったのですか? –