The second database created using MVC3, how to prevent?

I am very new to MVC 3 and I have problems with the Code First approach. I created 2 models, context, view, etc., and everything works fine (you can add, delete, etc.)

But when I check Microsoft SQL Management Studio, I see 2 databases. One of them is the one I asked in my web.config. It contains only a membership table

<connectionStrings> <add name="RecettesMaison" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=RecettesMaison;User Id=user;Password=password;" providerName="System.Data.SqlClient"/> 

The second has a strange name: "RecettesMaison.DB.RecettesMaisonContext". This is the one that contains the generated table ...

I would like to know where this name comes from, and how I can do it, the entire generated table goes to my connection string with only the specified name. I just want one database. I am sure that this is only one tag somewhere in the context, but I can not find this information in google.

EDIT: In nutshel, I want my database to continue to use DropCreateDatabaseIfModelChanges<RecettesMaisonContext> , but I want it to create a database with the name specified in the connection string, which is wrong (It creates a name using the namespace and the name of the context of the database data)

Thanks!

+4
source share
2 answers

This is because the connectionString that you did not find a file for your database, so CodeFirst automatically creates automatic use, using the full namespace, where is your DbContext for the file name.

Paste this text into connectionString by changing filename.mdf to the desired file name: AttachDBFilename=|DataDirectory|filename.mdf . The database file will be located in the project \ App_Data strong> folder .

you will have something like this:

 <connectionStrings> <add name="RecettesMaison" connectionString="Data Source=.\SQLEXPRESS;Initial catalog=RecettesMaison;AttachDBFilename=|DataDirectory|filename.mdf;User Id=user;Password=password;" providerName="System.Data.SqlClient"/> </connectionStrings> 

Make sure your DbContext uses the correct connectionString by passing connectionStringName in the DbContext constructor as follows:

 public class RecettesMaisonContext : DbContext { public RecettesMaisonContext() : base("RecettesMaison") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { var typesToRegister = Assembly.GetExecutingAssembly().GetTypes().Where( type => type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); foreach (object configurationInstance in typesToRegister.Select(Activator.CreateInstance)) { modelBuilder.Configurations.Add((dynamic)configurationInstance); } } ... } 
+1
source

By default, the code will first create a database for you. If you, however, want to override this behavior, just use

 Database.SetInitializer<YourDataContext>(null); 

MSDN: msdn.microsoft.com/en-us/library/gg679461 (v = vs .103) .aspx

+1
source

All Articles