EF 4.1 Code First:类型中的每个属性名称必须是唯一的查找表关联错误

发布于 2024-11-17 18:53:11 字数 1163 浏览 5 评论 0原文

这是我第一次尝试创建自己的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-11-24 18:53:11

您正在寻找:

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);

You are looking for:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文