C# EF 4.1代码优先:类型中的每个属性名称必须是查找表关联上的唯一错误
这是我第一次尝试创建自己的EF模型,我发现自己在尝试先使用代码创建查找表关联时遇到了困难,因此我可以访问:C# EF 4.1代码优先:类型中的每个属性名称必须是查找表关联上的唯一错误,c#,entity-framework,entity-framework-4,entity-relationship,C#,Entity Framework,Entity Framework 4,Entity Relationship,这是我第一次尝试创建自己的EF模型,我发现自己在尝试先使用代码创建查找表关联时遇到了困难,因此我可以访问: myProduct.Category.AltCategoryID 据我所知,我已经设置了正确的模型和映射,但仍在继续 错误0019:类型中的每个属性名称必须是唯一的。已定义属性名称“CategoryID” 以下模型在我的代码中表示: [Table("Product", Schema="mySchema")] public class Product { [Key, Databas
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"));
modelBuilder.Entity()
.has可选(p=>p.Category)
.WithRequired()
.Map(m=>m.MapKey(“CategoryID”);
我尝试了其他一些方法,包括添加[ForeignKey]注释,但这会导致包含对ProductID字段引用的错误。您正在查找:
modelBuilder.Entity<Product>()
// Product must have category (CategoryId is not nullable)
.HasRequired(p => p.Category)
// Category can have many products
.WithMany()
// Product exposes FK to category
.HasForeignKey(p => p.CategoryID);
modelBuilder.Entity()
//产品必须具有类别(CategoryId不可为空)
.HasRequired(p=>p.Category)
//类别可以有很多产品
.有很多
//产品将FK暴露于类别中
.HasForeignKey(p=>p.CategoryID);