0

エンティティフレームワークのコードで、継承を使用して共通エンティティソリューションと1対多の関係を実装しようとしています。それは望ましい結果を作りません。私はこのスキーマを必要とする:エンティティフレームワーク:1対多の関係の共通エンティティ

CommonEntity Address   Licensee User 
-------------- ------------- ----------- --------- 
CID(PK)   AddressID(PK) CID(FK/PK) CID(FK/PK) 
AddressID(FK) 

CommonEntity:

public class CommonEntity{ 
    [key] 
    public int CID{get;set;} 
    public virtual List<Address> Addresses { get; set; } 
} 

住所:

public class Address{ 
    [key] 
    public int AddressID{get;set;} 
    public string country{get;set;} 
} 

ライセンシー:

public class Licensee{ 
    [key] 
    [ForeignKey("CommonEntity")] 
    public int CID{get;set;} 
    public string CompanyName{get;set;} 
} 

ユーザー:

public class User{ 
    [key] 
    [ForeignKey("CommonEntity")] 
    public int CID{get;set;} 
    public string UserName{get;set;} 
    public string Pass{get;set;} 
} 

DbContextクラス:上記のコード

public class DataModelContext : DbContext 
{ 
    public DbSet<CommonEntity> CommonEntity { get; set; } 
} 

はこの例外を与えている:

ForeignKeyAttributeは、プロパティに 'CommonID​​' タイプ に 'TestingEF.Models.Licensee' は有効ではありません。ナビゲーションプロパティ 'CommonEntity'が依存型 'TestingEF.Models.Licensee'に見つかりませんでした。 [名前]の値は有効な ナビゲーションプロパティ名である必要があります。

+0

あなたは '[ForeignKeyの( "CommonEntity")]を使用'、あなたはまた、ナビゲーションプロパティ 'パブリック仮想CommonEntity CommonEntityを{取得する必要があります。セット; } '。 –

答えて

0

私は自分自身で解決策を見つけました。 モデル:

public class CommonEntity{ 
    [key] 
    public int CID{get;set;} 

    public long AddressId { get; set; } 

    [ForeignKey("Address")] 
    public virtual List<Address> Addresses { get; set; } 
} 
public class Address{ 
    [key] 
    public int AddressID{get;set;} 
    public string country{get;set;} 
    public virtual ICollection<CommonEntity> CommonEntity { get; set; } 
} 
[Table("Licensee")] 
public class Licensee{ 
    public string CompanyName{get;set;} 
} 
[Table("User")] 
public class User{ 
    public string UserName{get;set;} 
    public string Pass{get;set;} 
} 

Dbcontextクラス:

public class DataModelContext : DbContext 
{ 
     public DbSet<CommonEntity> CommonEntity { get; set; } 
     public DbSet<Address> Addresses { get; set; } 
} 
+0

これはあなたの質問に投稿されたモデルとはまったく異なります。上記のモデルでは、ライセンシー/ユーザークラスはモデルの一部ではありません。 'CommonEntity'と' Address'の間に2対1の関係を作成しています – Smit

+0

はい、私のモデルレイアウトに問題があり、それを見つけて修正しました。 –

関連する問題