c# - Can't save nested object in entity framework -



c# - Can't save nested object in entity framework -

i'm new in asp.net mvc. utilize standart membership provider in project. have application user , applicationdbcontext

public class applicationuser : identityuser { [required] public virtual userinfo userinfo { get; set; } public virtual icollection<lot> lots { get; set; } } public class applicationdbcontext : identitydbcontext<applicationuser> { public dbset<lot> lots { get; set; } public dbset<product> products { get; set; } public dbset<userinfo> userinfoes { get; set; } public dbset<personalinformation> personalinformations { get; set; } public applicationdbcontext() : base("defaultconnection") { } }

and userinfo

public class userinfo { [key] public int userinfoid { get; set; } public datetime registarationdate { get; set; } public decimal money { get; set; } public decimal currentdebt { get; set; } public virtual personalinformation personalinformation { get; set; } public virtual applicationuser applicationuser { get; set; } }

personal information

public class personalinformation { public int personalinformationid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string telephone { get; set; } public adress adress { get; set; } }

my registerviewmodel

public class registerviewmodel { [required] [display(name = "user name")] public string username { get; set; } [required] [stringlength(100, errormessage = "the {0} must @ to the lowest degree {2} characters long.", minimumlength = 6)] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [datatype(datatype.password)] [display(name = "confirm password")] [compare("password", errormessage = "the password , confirmation password not match.")] public string confirmpassword { get; set; } public string firstname { get; set; } public string lastname { get; set; } public decimal money { get; set; } public string telephone { get; set; } }

and action create in controller

[httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> register(registerviewmodel model) { if (modelstate.isvalid) { var user = new applicationuser() { username = model.username, userinfo = new userinfo() { currentdebt = 0, money = model.money, registarationdate = datetime.now, personalinformation = new personalinformation() { firstname = model.firstname, lastname = model.lastname, telephone = model.telephone, } } }; var result = await usermanager.createasync(user, model.password); if (result.succeeded) { await signinasync(user, ispersistent: false); homecoming redirecttoaction("index", "home"); } else { adderrors(result); } } // if got far, failed, redisplay form homecoming view(model); }

and when fill fields that're mentioned in action, receivee error

cannot insert value null column 'userinfoid', table 'defaultconnection.dbo.userinfoes'; column not allow nulls. insert fails. statement has been terminated.

could help me? lot

you need tell ef userinfoid key against table maps identity.

something work in mapping file:

public class userinfomap : entitytypeconfiguration<userinfo> { public userinfomap() { totable("userinfo"); haskey(m => m.userinfoid ); } }

when code first migration runs, generates this:

createtable( "dbo.userinfo", c => new { userinfoid = c.int(nullable: false, identity: true) )

which means won't need specify id auto populate in database.

c# asp.net-mvc entity-framework

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -