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