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") );
....