EF 4.1 Code First:类型中的每个属性名称必须是唯一的查找表关联错误
这是我第一次尝试创建自己的 EF 模型,我发现自己在尝试使用 Code First 创建查找表关联时陷入困境,这样我就可以访问:
myProduct.Category.AltCategoryID
我已经设置了模型和映射,据我所知是正确的,但继续得到 错误 0019:类型中的每个属性名称必须是唯一的。属性名称“CategoryID”已定义
以下模型在我的代码中表示:
[Table("Product", Schema="mySchema")]
public class Product {
[Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
public int ProductID { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
[Table("Category", Schema="mySchema")]
public class Category {
[Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string Name { get; set; }
public int AltCategoryID { get; set; }
}
我已指定与以下内容的关联:
modelBuilder.Entity<Product>()
.HasOptional(p => p.Category)
.WithRequired()
.Map(m => m.MapKey("CategoryID"));
我尝试了一些其他操作,包括添加 [ForeignKey] 注释,但是这会导致包含对 ProductID 字段的引用的错误。
This is my first attempting at creating my own EF model, and I'm finding myself stuck attempting to create a lookup table association using Code First so I can access:
myProduct.Category.AltCategoryID
I have setup models and mappings as I understand to be correct, but continue to get
error 0019: Each property name in a type must be unique. Property name 'CategoryID' was already defined
The following models are represented in my code:
[Table("Product", Schema="mySchema")]
public class Product {
[Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
public int ProductID { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
[Table("Category", Schema="mySchema")]
public class Category {
[Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.None)]
public int CategoryID { get; set; }
public string Name { get; set; }
public int AltCategoryID { get; set; }
}
I have specified the associations with:
modelBuilder.Entity<Product>()
.HasOptional(p => p.Category)
.WithRequired()
.Map(m => m.MapKey("CategoryID"));
I've tried a few other things, including adding the [ForeignKey] annotation, but that results in an error containing a reference to the ProductID field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找:
You are looking for: