0

私はEFを初めて使用します(基本的には始めたばかりです)。フォローに問題があります。製品を説明するテーブルがあるとしましょう。この製品 (タイプに基づく) は、いくつかの追加プロパティを持つことができます (この問い合わせの目的のために、2 つに制限します)。

class Product
{ 
    [Key]
    [Column("si_key")] 
    public Guid Key { get; set; }

    [Column("si_Name")] 
    public string Name {get; set; }

    [Column("si_Type")] 
    public TypeEnum Type { get; set; }

    [Column("si_PaperType")] 
    public Guid? PaperType { get; set };

    [Column("si_FoilType")] 
    public Guid? FoilType { get; set };

    // Mappings
    public PaperType PType { get; set; }
    public FoilType FType { get; set; }
}

class FoilType
{ 
    [Key]
    [Column("ft_key")] 
    public Guid Key { get; set; }

    [Column("ft_Name")] 
    public string Name {get; set; }
}

class PaperType
{ 
     [Key]
     [Column("pt_key")] 
     public Guid Key { get; set; }

     [Column("pt_Name")] 
     public string Name {get; set; }
 }

つまり、実際には、製品と (紙と箔タイプ) の間の 0-1 の関係について話しているのです。

流暢な API を使用してそれを定義する方法は? 私は使用しようとしていました:

modelBuilder.Entity<Product>()
  .HasOptional(u => u.PType)
  .WithOptionalPrincipal()
  .Map( m => m.MapKey("pt_guid") );

....

4

2 に答える 2

0
class Product
{ 
   [Key]
[Column("si_key")] 
public Guid Key { get; set; }

[Column("si_Name")] 
public string Name {get; set; }

[Column("si_Type")] 
public TypeEnum Type { get; set; }

//[Column("si_PaperType")] 
//public Guid? PaperType { get; set };/* remove this line*/

//[Column("si_FoilType")] 
//public Guid? FoilType { get; set };/* remove this line*/

// Mappings
public PaperType PType { get; set; }
public FoilType FType { get; set; }
}


  modelBuilder.Entity< Product >()
        .HasOptional< u.PType >(u => u.PType)
        .WithOptionalDependent(c => c.Product).Map(p =>        p.MapKey("PTypeId"));
于 2015-08-03T05:31:19.583 に答える
0

両側がオプションであることを意味するため、WithOptionalPrincipal は使用できません。

リレーションシップを optional:optional に構成します。リレーションシップの反対側にナビゲーション プロパティはありません。構成されているエンティティ タイプは、関係のプリンシパルになります。リレーションシップが対象とするエンティティ タイプは従属であり、プリンシパルへの外部キーが含まれます。

あなたが持っている唯一のオプションは、いわゆる1-1です。

class PaperType
{ 
     [Key]
     [Column("pt_key")] 
     public Guid Key { get; set; }

     [Column("pt_Name")] 
     public string Name {get; set; }

     // Mappings
     public Product Product { get; set; }
 }

modelBuilder.Entity<Product>()
   .HasOptional(x => x.PType)
   .WithRequired(x => x.Product);
于 2014-10-16T19:32:38.320 に答える