entity framework - Fixing Model/Column mapping in Code First/EF6 -


first of all, shot myself in foot. i'm building test application (this work related, not school btw.) have model foreign key property

home_teamid 

that mapped column called

home_teamid in database. happy until refactored use id instead of id. didn't notice migration added column called home_teamid1 , storing data there instead of home_teamid (where want it.)

so is:

  1. drop column home_teamid1 (no problem, can that.)

  2. rename home_teamid home_teamid. (no problem, can that.)

  3. tell ef write data original column.

i've read how use database mappings in dbcontext, isn't i'm trying either (i.e., one-time thing, not need every time app runs.) (btw, there no .edmx file either.)

so that's question -- how tell ef write home_teamid field in domain model home_teamid column in table?

i should add i've done migration since it's not (necessarily) easy target 1 revision.

edit 1: ef writing same team id both home_teamid , home_teamid1 columns, although had made ..id1 file foreign key.

i've looked everywhere on project text "id1" (both text , binary unicode) , places shows in *_migration.cs files.

in meantime, i've tried steps 1 , 2 above. , (as expected) get:

innerexception: system.data.sqlclient.sqlexception hresult=-2146232060 message=invalid column name 'home_teamid1'. invalid column name 'visitors_teamid1'. 

edit 2:

i tried this:

  1. create brand new (blank database)
  2. excluded .cs files in migrations project
  3. add-migration initialrecreate
  4. looked in resulting .cs file , removed reference id1. (in fact, there two...where did come from??)
  5. looked in project , found 0 references id1.
  6. update-database
  7. ran project
  8. invalid column name 'home_teamid1'.

so problem isn't database itself.

it case of software outsmarting human. in "higher-level" gamesummary class, had:

public int gamesummaryid { get; set; } public int home_teamid { get; set; } public virtual team home { get; set; } public int visitors_teamid { get; set; } public virtual team visitors { get; set; } 

and in team class had:

public int teamid { get; set; } 

so ef creating 2 columns, 1 home_teamid (the field home_teamid in gamesummary class) , 1 home_teamid (the foreign key navigation property pointed team object). solution:

public int gamesummaryid { get; set; } public int hometeamid { get; set; } public virtual team home { get; set; } public int visitorsteamid { get; set; } public virtual team visitors { get; set; } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -