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:
drop column home_teamid1 (no problem, can that.)
rename home_teamid home_teamid. (no problem, can that.)
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:
- create brand new (blank database)
- excluded .cs files in migrations project
- add-migration initialrecreate
- looked in resulting .cs file , removed reference id1. (in fact, there two...where did come from??)
- looked in project , found 0 references id1.
- update-database
- ran project
- 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
Post a Comment