2017-11-17 4 views
2

次のクラスがあることを考慮してください。EF6.x CodeFirst 1対多の関係

ProductCategory

FishProduct

NonFishProduct

A ProductCategoryは、多くのFishProductsおよびまたは多くのNonFishProducts、多くの関係に非常に簡単1を持つことができます。私の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; } 

私はその後、(3クラス用DbSetsで)私のコンテキストを追加し、それは私の三つのクラスを構築し、正しい関係を推論するべき移行を追加することにラウンドを取得します。私はアドオンの移行段階

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

中に次のエラーを取得するのに代わりに、私はこのエラーに行った検索の全ては、名前の先頭にアンダースコア、I避難所を使用した人を指しているように見えますcodefirstで1対多の関係を作成する方法を見てきたすべての例は、これらの行に沿っているようです。

私がここで間違って行ったことに誰かが光を当てることはできますか?ここでの要求に応じて

EDIT

はFishProductクラス

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

    } 
} 

のための完全なコードでFishProductとは、マップされません。私は、基本クラスを作成し、いくつかの共通要素を(共有NonFishProductので、テーブルに)

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 

答えて

2

PostSharpを使用していますか?

この質問によると、PostSharpはプロパティ名に "k__BackingField"を挿入するのが好きです。質問に対する答えは、解決策に向けたいくつかの示唆を提供する。

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

+0

あなたはそれに答えを要求した編集から見ることができ尻ははい、非常によく私が追加される場合があります発見されます。 –

+0

うれしい私は助けることができました! – Adosi

+2

今後これに来る人の利益のために。 Adosiは、PostSharpを使用していた問題(ただし、PSは使用しないとあまりにも良い)が問題であるという点では絶対的に気付いていました。彼が指摘したリンクは、それほど答えではありませんでした(ただし、その質問に出てきたかもしれませんが)。私の場合は、このプロパティに次のPostSharp属性[IgnoreAutoChangeNotification]を追加します。 'public virtual ProductCategory ProductCategory {get;セット; } '両方のクラスでは、このトリックを受けました。 –