c# - How to specify foreign key property for one to zero..one relation using fluent api in Entity Framework -


i have table user , table company. user can have 0 or 1 company registered.

user (1)---> (0..1) company 

my user class:

public class user {     public string id {get; set; }     public string firstname { get; set; }     public string lastname { get; set; }      public string fullname     {         { return firstname + " " + lastname; }     }      //relations     public virtual company company { get; set; }  } 

and company class is:

public class company {     public int id { get; set; }             public string companyname { get; set; }     public string taxnumber { get; set; }     public string taxoffice { get; set; }     public string officetel { get; set; }     public string faxnumber { get; set; }     public string website { get; set; }     public string address { get; set; }     public string { get; set; }      //keys     public int cityid { get; set; }     public int stateid { get; set; }                     public string userid { get; set; }      //relations     public city city { get; set; }     public state state { get; set; }             public user user { get; set; } } 

the fluent api used company this:

public class companyconfiguration : entitytypeconfiguration<company> {     public companyconfiguration()     {         this.hasrequired(x => x.user)             .withoptional(x => x.company);          this.hasrequired(x => x.city)             .withmany(x => x.companies).hasforeignkey(x => x.cityid).willcascadeondelete(false);          this.hasrequired(x => x.state)             .withmany(x => x.companies).hasforeignkey(x => x.stateid).willcascadeondelete(false);          this.property(x => x.address).hasmaxlength(400);         this.property(x => x.companyname).hasmaxlength(100).isrequired();         this.property(x => x.faxnumber).hasmaxlength(20);         this.property(x => x.officetel).hasmaxlength(20);         this.property(x => x.taxnumber).hasmaxlength(20).isrequired();         this.property(x => x.taxoffice).hasmaxlength(50).isrequired();         this.property(x => x.website).hasmaxlength(200);                 } } 

after run add-migration expect userid used foreign key user in company table, generated entity framework migrations is:

createtable(             "dbo.companies",             c => new                 {                     id = c.int(nullable: false, identity: true),                     companyname = c.string(nullable: false, maxlength: 100),                     taxnumber = c.string(nullable: false, maxlength: 20),                     taxoffice = c.string(nullable: false, maxlength: 50),                     officetel = c.string(maxlength: 20),                     faxnumber = c.string(maxlength: 20),                     website = c.string(maxlength: 200),                     address = c.string(maxlength: 400),                     = c.string(),                     cityid = c.int(nullable: false),                     stateid = c.int(nullable: false),                     userid = c.string(),                     user_id = c.string(nullable: false, maxlength: 128),                 })             .primarykey(t => t.id)             .foreignkey("dbo.cities", t => t.cityid)             .foreignkey("dbo.states", t => t.stateid)             .foreignkey("dbo.aspnetusers", t => t.user_id)             .index(t => t.cityid)             .index(t => t.stateid)             .index(t => t.user_id); 

question how can force entity framework use specified property foreign key relation, , reason need userid value of company in code, , don't want use company.user.id expression that.

note: use entity framework 6.1.2 , asp.net mvc 5

here how define foreignkey in relationship:

public class company {    public int id { get; set; }             [required]    [key, foreignkey("user")]    public string userid { get; set; }            public user user { get; set; } } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -