asp.net mvc 3 - Entity framework is making the id null on insert -
i'm getting following error when try insert new row in 1 of relational tables. have following 2 models:
public class companycredit { [key] [databasegenerated(databasegeneratedoption.identity)] public int creditid { get; set; } public int plancredit { get; set; } public datetime? plancreditexpirationdate { get; set; } }
and
public class companyinformation { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int id { get; set; } [required] [displayname("company name:")] public string companyname { get; set; } public string timezone { get; set; } //navigation properties public virtual companycredit credits { get; set; } }
and relation in dbcontext
modelbuilder.entity<companyinformation>().hasoptional(e => e.credits);
i'm trying add record inside companycredit table so:
if (_company.credits == null) { var _credits = new companycredit(); _credits.plancredit = 200; _credits.plancreditexpirationdate = system.datetime.utcnow.addmonths(1); _company.credits = _credits; repo.insertorupdate(_company, user.identity.name); }
and insert or update marks company changed , _credit added so:
_db.entry(_credits).state = system.data.entitystate.added; _db.entry(company).state = system.data.entitystate.modified; _db.savechanges();
when runs following error can't seem find reason to.
cannot insert value null column 'creditid', table 'project.dbo.companycredits'; column not allow nulls. insert fails.
the statement has been terminated.
thank in advanced help.
i found problem in attribute [databasegenerated(databasegeneratedoption.identity)]
should have been [databasegeneratedattribute(databasegeneratedoption.identity)]
thought post others might benefit it.
Comments
Post a Comment