c# - Entity Framework - How to map a required property on one side with no navigation property on other side -


i have following 2 pocos (product , user) , complex type (tracking).

class product() {      guid id { get; protected set; }     tracking trackinginfo { get; protected set; }      // other properties }   class user() {      guid id { get; protected set; }      // other properties }   class tracking() {        user createdby { get; protected set; }       guid createdbyid { get; protected set; }        // other properties } 

the tracking class wrapper tracking info , contains other tracking properties (date created, updated etc) , fulfils interface other purposes, concerned mapping relationships between product trackinginfo , user.

every product must have associated user maps trackinginfo.createdby property.

the catch don't want create navigation properties on user product - i.e. don't want have icollection<product> productscreated property.

i'm not sure how relationship or complex type mapping should done in entityframework code first. have mapping class follows:

public class productmap : entitytypeconfiguration<product> {     public productmap()     {          this.property(t => t.id).hasdatabasegeneratedoption(databasegeneratedoption.identity).hascolumnname("id");           // complex type mapping here?     }  } 

how can

  1. map trackinginfo complex type
  2. map createdby -> user single direction relationship?

complex types cannot contain navigation properties. recommend creating abstract base class containing tracking properties configuration:

public abstract class tracking {     public guid createdbyid { get; set; }     public virtual user createdby { get; set; } }  public abstract class trackingconfig<t> : entitytypeconfiguration<t> t: tracking {     public trackingconfig()     {         hasrequired( t => t.createdby )             .withmany()             .hasforeignkey( t => t.createdbyid );     } }  public class product : tracking {     public guid id { get; set; } }  public class productconfig : trackingconfig<product> {     public productconfig()     {     } } 

...

    protected override void onmodelcreating( dbmodelbuilder modelbuilder )     {         base.onmodelcreating( modelbuilder );          modelbuilder.configurations.add( new productconfig() ); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -