0

I am quite new new to EF (basically just starting). I am having problems with following. Lets say that I have a table that describes a product, this product (based on type) can have a number of additional properties ( for a purpose of this enquiry I will limit it to two).

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

So really we are talking about 0-1 Relationship between Product and (paper and foilType).

How to define it using fluent API? I was trying to use:

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

....

Borys Generalov
  • 2,255
  • 17
  • 19
SebSky
  • 161
  • 1
  • 7

2 Answers2

0

You can not use WithOptionalPrincipal as it implies that both sides are optional.

Configures the relationship to be optional:optional without a navigation property on the other side of the relationship. The entity type being configured will be the principal in the relationship. The entity type that the relationship targets will be the dependent and contain a foreign key to the principal.

The only option you have is so called 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);
Borys Generalov
  • 2,255
  • 17
  • 19
  • Problem is that PaperType can be also used by another entities so I dont want to have a Product navigation property (or list) on the other side. – SebSky Oct 16 '14 at 22:05
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"));
Rakib
  • 643
  • 7
  • 17