c# 4.0 - Entity Framework WebAPI Model UpSerts -


i'm having problems duplicate data during migration code first.

a new foreign key record duplicated each time migration creates master record.

the schemas in database being created correctly. namely primary keys , foreign key values (the latter being automatically generated)

can please advise how detach foreign key record during migration prevent recreating record or configuration need implement? i've tried updating state of foreign key obects before inserting master data. both modified , detached.

for example see multi records same priority there should 3.

i'm using entity framework 6.0.

public class velopointdbconfiguration : dbmigrationsconfiguration<velopointdbcontext> {     public velopointdbconfiguration()     {         automaticmigrationsenabled = true;         automaticmigrationdatalossallowed = true;     }     protected override void seed(velopointdbcontext context)     {       context.taskpriorities.addorupdate(eventtaskpriority.migrations.all());         context.taskstatuses.addorupdate(taskstatus.migrations.all());           eventorganisertask.migrations.all().select(x => context.entry(x.priority).state == entitystate.modified);         eventorganisertask.migrations.all().select(x => context.entry(x.taskstatus).state == entitystate.modified);          context.tasks.addorupdate(eventorganisertask.migrations.all());     } } 

the following examples of instances i'm using data.

i create following methods foreign key objects

public static eventtasktype[] all() {     return new[]     {         getdeadline(),         getemail(),         gettelephone(),         getappointment(),         getsnailmail(),     }; } internal static eventtasktype getdeadline() {     return new eventtasktype("09974722-d03e-4ca3-bf3a-0af7f6ca1b67", 1, "deadline")     {         icon = ""     } } 

i call following methods create master data.

public static eventorganisertask[] all() {     return new eventorganisertask[]     {         getbookhq(1, new datetime(event.migrations.eventdate.year - 1, 10, 1)),         getfindsponsor(2, new datetime(event.migrations.eventdate.year - 1, 10, 1)),         getregisterevent(3, new datetime(event.migrations.eventdate.year - 1, 10, 1)),         getbookfirstaid(4, event.migrations.eventdate.addmonths(-6))     }; } 

note: when creating master record, call method in foreign key classes each time - crux of problem need instruct migration detach item.

public static eventorganisertask getregisterevent(int id, datetime date) {     return new eventorganisertask     {         id = id,         title = "register event",         summary = "register road race region",         duedate = date,         priority = eventtaskpriority.migrations.gethighpriority(),         person = person.migrations.getraceorganiser(1),         tasktype = eventtasktype.migrations.getdefault(),         taskstatus = taskstatus.migrations.getdefault(),     }; } 

note: when make changes data application, foreign keys not being updated. must related , indicates entities not configured correctly.

latest:

i'm still tearing hair out. i've investigated further , read migrations being multi threaded (it thread on stackoverflow can't find again). indeed running seed method supposed says on tin , purley seeding data, data being added (regardless of addorupdate - what's then) i've looked @ behaviour regarding records being created. first of called context.savechanges() after creating tables. @ point doesn't created duplicates items referenced once. let seed method run master data - argggh - see duplicates (when instances called on master data). did flag regard order in creates records.

my next step create 2 migrations, without success.

i'm hoping picks thread soon. i'm tearing hair out.

ok i've found answer. clever enough create foreign key relationships model, needed explicitly set foreign key id field. chose fluent api explicitly set relationships , set value of id field in mapping of object.

modelbuilder.entity<task>()                     .hasrequired(x => x.priority)                     .withmany(x => x.tasks)                     .hasforeignkey(x => x.priority_id); 

here in seed method

    public class velopointdbconfiguration : dbmigrationsconfiguration<velopointdbcontext>     {         public velopointdbconfiguration()         {             automaticmigrationsenabled = true;             automaticmigrationdatalossallowed = true;         }          protected override void seed(velopointdbcontext context)         {        context.taskpriorities.addorupdate(eventtaskpriority.migrations.all());                 context.taskstatuses.addorupdate(taskstatus.migrations.all());             eventorganisertask.migrations.all().select(x => context.entry(x.priority).state == entitystate.modified);             eventorganisertask.migrations.all().select(x => context.entry(x.taskstatus).state == entitystate.modified);              context.tasks.addorupdate(eventorganisertask.migrations.all());             // foreign key relationships             modelbuilder.entity<eventorganisertask>()                     .hasrequired(x => x.taskstatus)                     .withmany(x => x.tasks)                     .hasforeignkey(x => x.taskstatus_id);                 modelbuilder.entity<task>()                     .hasrequired(x => x.tasktype)                     .withmany(x => x.tasks)                     .hasforeignkey(x => x.tasktype_id);                 modelbuilder.entity<task>()                     .hasrequired(x => x.priority)                     .withmany(x => x.tasks)                     .hasforeignkey(x => x.priority_id);          }     } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -