c# - EntityFramwork 6 - ID not set when calling SaveChanges() -
i got little problem entityframework. setup follows : using code-first migrations , database structure :
using microsoft.azure.mobile.server; namespace myproject.dataobjects { public class listrelations : entitydata { public string userid { get; set; } public bool read { get; set; } public bool write { get; set; } } }
now inherting enititydata should add id field , meta data stuff, right ? when try populate database :
var relations = new listrelations(); relations.userid = currentuser; relations.read = true; relations.write = true; relations.listid = current.id; context.listrelationsset.add(relations); try { context.savechanges(); } catch (dbentityvalidationexception dbex) { foreach (var validationerrors in dbex.entityvalidationerrors) { foreach (var validationerror in validationerrors.validationerrors) { trace.traceinformation("property: {0} error: {1}", validationerror.propertyname, validationerror.errormessage); } } }
i getting errors, saying id field needed populated. but, shouldnt database me ? bit confused, welcoming help!
use
public class listrelations : entitydata { [databasegenerated(databasegeneratedoption.identity), column(typename = "varchar"), maxlength(20), key] public string userid { get; set; } public bool read { get; set; } public bool write { get; set; } }
it depends on database provider, instance t-sql has procedure called newsequentialid()
populate these kind of columns.
sample use in migration be
id = c.guid(nullable: false, identity: true, defaultvaluesql: "newsequentialid()"),
Comments
Post a Comment