2

Consider that I have following classes;

ProductCategory

FishProduct

NonFishProduct

A ProductCategory can have many FishProducts and or many NonFishProducts, a pretty straightforward one to many relationship. In my ProductCategory Class I have the following;

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

    }


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

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

and in FishProduct and NonFishProduct I have the following;

public int ProductCategoryId { get; set; }

    public virtual ProductCategory ProductCategory { get; set; }

Logically when I then add my context (with DbSets for the three classes) and get round to adding a migration it ought to build my three classes and infer the correct relationship. Instead though I get the following error during the add-migration step

<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'.

All of the searches I've done on this error seem to point to people having used an underscore at the start of a name, which I haven't, and every example I have seen of how to create a one to many relationship in codefirst seems to follow along these lines.

Can anyone shed some light on what I have done incorrectly here.

EDIT

In response to a request here is the full code for the FishProduct Class

   namespace SalesAndPurchases
{
    [Table("FishProducts", Schema = "SalesAndPurchases")]
    public class FishProduct : ProductBase
    {
        [StringLength(3)]
        public string SpeciesCode { get; set; }

        public string FreshnessCode { get; set; }

        [StringLength(3)]
        public string StateCode { get; set; }

        [StringLength(3)]
        public string PresentationCode { get; set; }

        [StringLength(1)]
        public string SizeCode { get; set; }

        public int ProductCategoryId { get; set; }

        public virtual ProductCategory ProductCategory { get; set; }

    }
}

because FishProduct and NonFishProduct share some common elements I created a base class (which doesn't get mapped to a table)

namespace SalesAndPurchases
{
    public abstract class ProductBase : VtlEntityBase
    {
        public string ProductDescription { get; set; }
        public string IntrastatCode { get; set; }


    }
}

and for the sake of completeness here is the base class for all entities across all schemas;

    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
Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47

1 Answers1

2

Are you using PostSharp?

According to this question, PostSharp likes to insert "k__BackingField" to property names. The answer to the question offers some suggestions towards a solution.

PostSharp inserting k__Backing Field into Entity Class, causing Database generation to fail

Omar Himada
  • 2,540
  • 1
  • 14
  • 31
  • Ass you can see from the edit you requested the answer to that is yes, very well spotted I might add. – Dom Sinclair Nov 17 '17 at 14:33
  • Glad I could help! – Omar Himada Nov 17 '17 at 14:33
  • 2
    For the benefit of anyone coming to this in the future. Adosi was absolutely spot on that the issue was my using PostSharp (but PS is too good to not use). The link he pointed to wasn't quite the answer though (although it might have been to that question). in my case adding the following PostSharp attribute [IgnoreAutoChangeNotification] to this property 'public virtual ProductCategory ProductCategory { get; set; }' in both classes affected did the trick. – Dom Sinclair Nov 17 '17 at 15:19