.net - Entity Framework Code-First: How to manually update the database? -



.net - Entity Framework Code-First: How to manually update the database? -

i've build little wpf demo app uses ef code-first save info in sql ce 4.0 db. works fine unless remove property model object. example, if remove "hosteby" class.....

public class dinner { public int dinnerid { get; set; } public string title { get; set; } public datetime eventdate { get; set; } public string address { get; set; } public string hostedby { get; set; } public virtual icollection<rsvp> rsvps { get; set; } }

...it throws exception:

the model backing 'nerddinners' context has changed since database created. either manually delete/update database, or phone call database.setinitializer idatabaseinitializer instance. example, dropcreatedatabaseifmodelchanges strategy automatically delete , recreate database, , optionally seed new data.

the error persists after removing field "hosteby" manually database. missing here? have delete/truncate db or there solution?

in first scenario changed code first model, before went , modified database manually, reply open (nuget) bundle manager console , type:

update-database -verbose

except - because in case removing column study it's delete something, , won't delete without explicitly saying that's ok. type:

update-database -f -verbose

now delete column had in model. -verbose says show sql runs. if you're scared of letting delete things , rather inspect sql before runs, use:

update-database -f -script

that instead dump sql out script can over, , run manually yourself.

in case went on , deleted column in database manually, have more complex scenario on hands; edmmetadata table described in other reply here contains hash of entire database not match database itself. can run manual sql homecoming db way entity framework expects (the way before manually modified it, brings in line hash) inspecting had before , db looks like.

if that's not feasible in ugliest part of entity framework code first. need eliminate hash table , reverse engineer db code files.

the hash table name depends on version of ef. in older ef4 asking about, it's called edmmetadata. in newer ef5, it's called __migrationhistory (under scheme tables in database if you're looking in sql server management studio). you'll need wipe out.

the news on sec step, reverse engineering science db code, microsoft has released tool beta you.

walk-through of reverse-engineering db, , ef powerfulness tools

you can skip many of first steps there since they're setting db , adding nonsense can demonstrate need do: reverse engineer db.

update:

it can feasible utilize manual migration work around scenario. create backup of db, run:

add-migration whateveryouwanttocallthis

the modifications db ef migrations need run appear in generated c# commands. it's tinker them both work around problems it's attempting (for illustration attempting delete columns have been deleted), , set place things need going forwards (for illustration adding table still have in model manually deleted in db).

once you've added , run update-database -f, ef code first take on faith you've updated database way needs be, , update hash based on end result. if made right changes can proceed migrations normal. if still causes errors can re-create commands of manual migration out somewhere , delete it, restore db backup, add together manual migration 1 time again , seek again. worst case resort reverse engineering science step above.

.net c#-4.0 entity-framework-4.1 ef-code-first sql-server-ce

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -