这里是我的模型无法隐式转换类型 '诠释' 到
public class Category
{
public int CategoryId { get; set; }
public string Description { get; set; }
public Category Parent { get; set; }
public ICollection<Category> Children { get; set; }
}
映射:
modelBuilder.Entity<Category>()
.HasMany(x => x.Children)
.WithOptional(y => y.Parent)
.Map(m => m.MapKey("ParentId"));
下面是如何添加数据:
var a = new List<Category>
{
new Category { Description = "Animals" }
new Category { Description = "Dog", Parent = 1 } <<<-ERROR
}
a.ForEach(s => context.Categories.Add(s));
context.SaveChanges();
如何添加从第一行到第二行的CategoryId?
对不起,我觉得这是一个非常基本的问题,但我不能弄清楚如何做到这一点
感谢
* I *假设你想“狗”的父母是‘动物’。是什么让*你*认为“动物”将成为具有'CategoryId'1的'Category'? – 2012-04-28 13:52:04
是的你是对的,狗的父母将是动物。 caategoryId是自动编号。对不起如果我的示例没有意义。这只是一个测试数据,而不是我使用的实际代码。让我知道你是否需要更多细节。感谢 – samantha07 2012-04-28 13:54:06