entity framework - Code First for SQL Server 2005? -


my development db 2008 r2 code first generates local database dropcreateifmodelchanges.

my method of deploying production generate scripts local db, including data , run in production. creates of tables, including edmmetadata table , populates hash.

works: run script in different 2008 r2 instance, change connection string entity model point production instance, run application.

does not work: run script in different 2005 instance, change connection string entity model point production instance, run application. error indicating model has changed.

i think doesn't work because db compatibility version part of hash. in production generates hash , compares hash stored in edmmetadata table. new hash different because generated against 2005 db.

i guessing not have problem if generating 2005 db locally , deploying 2005 production instance. don't have 2005 installed , rather not require developers have install it, when 2008 supports 2005 compatibility mode already.

how can force ef generate db in 2005 compatibility mode?

if problem model compatibility check, can disable database initializers context when running in production environment presumably don't need initialize database anyway. can in code so:

database.setinitializer<mycontext>(null); 

but it's better in app.config/web.config production application so:

<entityframework>   <contexts>     <context type="mynamespace.mycontext, myassembly" disabledatabaseinitialization="true" />   </contexts> </entityframework> 

you need update ef 4.3 syntax--see http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx. there way in ef 4.1: see http://blog.oneunicorn.com/2011/03/31/configuring-database-initializers-in-a-config-file/.

you try updating to ef 4.3 doesn't use edmmetadata table anymore--it uses __migrationhistory table instead. checks model compatibility in different way. may still flag difference if code first have generated different database 2005 did 2008, case.

you install sql server 2005 express on dev box. it's free , match production environment better.

finally, if none of above work , need force code first generate 2005 model/database, can that, means using lower-level building blocks. first, you'll need create dbmodelbuilder , call entity each of entity types have dbset declared on context:

var modelbuilder = new dbmodelbuilder(); modelbuilder.entity<user>(); modelbuilder.entity<blog>(); 

you can other fluent configuration here or use data annotations normal. onmodelcreating not called don't put fluent calls there--move them here instead.

once have configured dbmodelbuilder you'll need build , compile compiled model can passed dbcontext. @ stage can pass in "2005" provider manifest token.

var compiledmodel = modelbuilder     .build(new dbproviderinfo("system.data.sqlclient", "2005"))     .compile(); 

you should cache compiled model in app domain build , compile once. (normally dbcontext when builds model, if build model need caching yourself.)

finally, need pass compiled model constructor of context every time need use , have constructor pass model on base constructor.

public class mycontext : dbcontext {     public mycontext(dbcompiledmodel model)         : base(model)     {     }      public dbset<user> users { get; set; }     public dbset<blog> blogs { get; set; } } 

there other constructor overloads passing name or connection string if need them.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -