C# EF6.x CodeFirst一对多关系

C# EF6.x CodeFirst一对多关系,c#,entity-framework-6,ef-code-first,C#,Entity Framework 6,Ef Code First,考虑到我有以下课程 产品类别 鱼产品 非鱼类产品 一个产品类别可以有许多鱼产品和/或许多非鱼产品,这是一种非常简单的一对多关系。在我的ProductCategory类中,我有以下内容: public ProductCategory() { FishProducts = new HashSet<FishProduct>(); NonFishProducts = new HashSet<NonFishProduct>();

考虑到我有以下课程

产品类别

鱼产品

非鱼类产品

一个产品类别可以有许多鱼产品和/或许多非鱼产品,这是一种非常简单的一对多关系。在我的ProductCategory类中,我有以下内容:

   public ProductCategory()
    {
        FishProducts = new HashSet<FishProduct>();
        NonFishProducts = new HashSet<NonFishProduct>();

    }


    public ICollection<FishProduct> FishProducts { get; set; }

    public ICollection<NonFishProduct> NonFishProducts { get; set; }
public int ProductCategoryId { get; set; }

    public virtual ProductCategory ProductCategory { get; set; }
从逻辑上讲,当我添加上下文(三个类使用dbset)并开始添加迁移时,应该构建我的三个类并推断正确的关系。尽管我在添加迁移步骤中遇到以下错误

<ProductCategory>k__BackingField: Name: The specified name is not allowed: '<ProductCategory>k__BackingField'.
<ProductCategory>k__BackingField: Name: The specified name is not allowed: '<ProductCategory>k__BackingField'.
SalesAndPurchases.NonFishProduct_<ProductCategory>k__BackingField: Name: The specified name is not allowed: 'NonFishProduct_<ProductCategory>k__BackingField'.
NonFishProduct_<ProductCategory>k__BackingField_Source: Name: The specified name is not allowed: 'NonFishProduct_<ProductCategory>k__BackingField_Source'.
NonFishProduct_<ProductCategory>k__BackingField_Target: Name: The specified name is not allowed: 'NonFishProduct_<ProductCategory>k__BackingField_Target'.
SalesAndPurchases.FishProduct_<ProductCategory>k__BackingField: Name: The specified name is not allowed: 'FishProduct_<ProductCategory>k__BackingField'.
FishProduct_<ProductCategory>k__BackingField_Source: Name: The specified name is not allowed: 'FishProduct_<ProductCategory>k__BackingField_Source'.
FishProduct_<ProductCategory>k__BackingField_Target: Name: The specified name is not allowed: 'FishProduct_<ProductCategory>k__BackingField_Target'.
NonFishProduct_<ProductCategory>k__BackingField: Name: The specified name is not allowed: 'NonFishProduct_<ProductCategory>k__BackingField'.
FishProduct_<ProductCategory>k__BackingField: Name: The specified name is not allowed: 'FishProduct_<ProductCategory>k__BackingField'.
因为FishProduct和NonFishProduct共享一些公共元素,所以我创建了一个基类(它不会映射到表)

为了完整性,这里是所有模式中所有实体的基类

    namespace VtlCommon
{
    [NotifyPropertyChanged]
    public abstract class VtlEntityBase
    {
        [Key]
        public int Id { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateChanged { get; set; }

        [Timestamp]
        public byte[] RowVersion { get; set; }

    }
}



enter code here

你在用PostSharp吗

根据这个问题,PostSharp喜欢在属性名称中插入“k_ubackingfield”。这个问题的答案提供了一些解决方案的建议


Ass您可以从您要求的编辑中看到答案是肯定的,我可能会添加很好的发现。很高兴我能提供帮助!为了将来任何人的利益。Adosi绝对正确地指出了我使用PostSharp的问题(但是PS太好了,不能不使用)。不过,他所指的链接并不是完全正确的答案(尽管它可能是针对那个问题的)。在我的例子中,将以下PostSharp属性[IgnoreAutoChangeNotification]添加到受影响的两个类中的该属性“public virtual ProductCategory ProductCategory{get;set;}”中,成功了。
namespace SalesAndPurchases
{
    public abstract class ProductBase : VtlEntityBase
    {
        public string ProductDescription { get; set; }
        public string IntrastatCode { get; set; }


    }
}
    namespace VtlCommon
{
    [NotifyPropertyChanged]
    public abstract class VtlEntityBase
    {
        [Key]
        public int Id { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateChanged { get; set; }

        [Timestamp]
        public byte[] RowVersion { get; set; }

    }
}



enter code here