Entity Framework CTP4: where to put SetInitializer?

I am trying to add an Entity Framework, first code, to an MVC application that was run with test data using a CTP4 preview.

I am currently getting this error:

The model supporting the SchedulerContext context has changed since the database was created. Either manually delete / update the database, or call Database.SetInitializer with an instance of IDatabaseInitializer. For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database and possibly sow it with new data.

I do not want to generate a database at all, since I already have a database. Therefore, I tried to add the following to the SchedulerContext constructor:

Database.SetInitializer<SchedulerContext>(new CreateDatabaseOnlyIfNotExists<SchedulerContext>()); 

which had no effect - I got the same error the next time. The error occurs when it executes a LINQ statement that accesses the database - the first one, I think.

Where should I put this statement or is this statement generally the answer to this problem?

+6
c # entity-framework asp.net-mvc-2
source share
4 answers

Update

I was simply hushed up by the fact that you already have a database and you don't want to create another ... in this case, the answer is to put this in your SchedulerContext class

 protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) { modelBuilder.IncludeMetadataInDatabase = false; } 

Old answer

Usually you put it in the Global.asax file

 protected void Application_Start() { Database.SetInitializer<SchedulerContext>(new CreateDatabaseOnlyIfNotExists<SchedulerContext>()); } 

Note that it will only be initialized the first time you use a data context.

Update

 public class DataContextInitializer : CreateDatabaseOnlyIfNotExists<SchedulerContext> { protected override void Seed(SchedulerContext context) { } } 

Then you change the SetInitializer like this.

 System.Data.Entity.Infrastructure.Database.SetInitializer<SchedulerContext>(new DataContextInitializer()); 
+9
source share

Btw, with CTP5, is the right way to install the initializer in Global.asax

protected void Application_Start() { System.Data.Entity.Database.DbDatabase.SetInitializer<NerdDinners>(new System.Data.Entity.Database.DropCreateDatabaseIfModelChanges<NerdDinners>()); }

+4
source share

In EF 4.1

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); } 
+3
source share

The problem is that the ADO.NET command creates the connection as

  connectionBuilder = new SqlConnectionStringBuilder(sqlConnection.ConnectionString) { InitialCatalog = "master", AttachDBFilename = string.Empty, }; 

BUT sqlConnection.ConnectionString is a clean connection string specified in the application configuration file, but without a password! due to security reasons, I think ... therefore opening such a connection / STRONG>


The only possible solution I found is to use integrated security, which means to set the connection string property Integrated Security = SSPI

And we need to wait until there is a patch or CTP5; o)

+1
source share

All Articles