c# - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF -
i trying insert new activityinstance, not setting id, entity framework attempting insert id anyway.
the model creation , save command:
var ai = new activityinstance { activityid = 1, locationid = 1, begindate = datetime.now, expirationdate = datetime.now }; dbcontext.activityinstances.add(activityinstance); dbcontext.savechanges();
the model
public class activityinstance : ibusinessobject, ientity { public int id { get; set; } public int locationid { get; set; } public string targetid { get; set; } public int activityid { get; set; } public datetime? begindate { get; set; } public datetime? expirationdate { get; set; } public guid? durableinstanceworkflowid { get; set; } public int? triggeredbyid { get; set; } public virtual activity activity { get; set; } public virtual location location { get; set; } public virtual msdsdatavalidation msdsdatavalidation { get; set; } public virtual icollection<resultstransmittalquestionanswer> activityquestionanswers { get; set; } public virtual icollection<resultstransmittalcontact> resultstransmittalcontacts { get; set; } public virtual target target { get; set; } public virtual correctiveactionplan correctiveactionplans { get; set; } public virtual fmvisit fmvisit { get; set; } public virtual acknowledgereport acknowledgereport { get; set; } public virtual slcap slcap { get; set; } public virtual fmvisitstudentrecordreview fmvisitstudentrecordreview { get; set; } public virtual icollection<activityinstancereminder> activityinstancereminders { get; set; } // public virtual instancestable instances { get; set; } }
the mapping
public activityinstancemap() { totable("activityinstances"); haskey(x => x.id); property(t => t.id); property(t => t.locationid); property(t => t.targetid); property(t => t.activityid); property(t => t.begindate); property(t => t.expirationdate); property(t => t.durableinstanceworkflowid); property(t => t.triggeredbyid); hasoptional(x => x.target) .withmany(x => x.activityinstances) .hasforeignkey(x => x.targetid); hasrequired(t => t.activity) .withmany(t => t.activityinstances) .hasforeignkey(t => t.activityid); hasrequired(t => t.location) .withmany(t => t.activityinstances) .hasforeignkey(t => t.locationid); hasoptional(t => t.acknowledgereport) .withrequired(t => t.activityinstance); }
the table design
create table [dbo].[activityinstances] ( [id] int identity(1,1) not null, [durableinstanceworkflowid] uniqueidentifier null, [targetid] nvarchar(128) null, [locationid] int not null, [activityid] int not null, [begindate] datetime null, [expirationdate] datetime null, [triggeredbyid] int null, constraint [pk_activityinstances_id] primary key clustered ([id] asc), constraint [fk_activityinstances_tolocations] foreign key ([locationid]) references dbo.locations([id]), constraint [fk_activityinstances_toactivities] foreign key ([activityid]) references dbo.activities([id]), constraint [fkinstancetriggeredinstance] foreign key ([triggeredbyid]) references dbo.activityinstances([id]), constraint [fk_activityinstances_todurableworkflowinstance] foreign key ([durableinstanceworkflowid]) references [system.activities.durableinstancing].instancestable([id]) )
and getting in output window. id value default integer 0.
insert [dbo].[activityinstances]([id], [locationid], [targetid], [activityid], [begindate], [expirationdate], [durableinstanceworkflowid], [triggeredbyid]) values (@0, @1, null, @2, @3, @4, null, null)
update modified activityinstance model first suggested answer has led me toward actual issue, though still have not solved it. recommendation modify activityinstance model setting below on id
[databasegenerated(databasegeneratedoption.identity)] public int id { get; set; }
now getting error:
a dependent property in referentialconstraint mapped store-generated column. column: 'id'.
i looking @ optional 1 one relationship currently. acknowledge report table is:
create table [dbo].[acknowledgereports] ( [activityinstanceid] int not null, constraint [pk_acknowledgereportsid] primary key clustered ([activityinstanceid] asc) )
acknowledgereport model
public class acknowledgereport : iactivityinstance { public int activityinstanceid { get; set; } public virtual list<report> reports { get; set; } public virtual activityinstance activityinstance { get; set; } }
and acknowledgereport mapping
public acknowledgereportmap() { totable("acknowledgereports"); haskey(t => t.activityinstanceid); property(t => t.activityinstanceid); // relationships }
try adding attributes let know database generated
[key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int id { get; set; }
Comments
Post a Comment