Segunda base de datos creada con MVC3, ¿cómo prevenir?

I'm very new to MVC 3 and I'm having problem with the Code First approach. I created 2 models, a context, view, etc and everything works perfect (able to add, delete, etc.)

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

<add name="RecettesMaison" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=RecettesMaison;User Id=user;Password=password;"

The second one has a strange name: "RecettesMaison.DB.RecettesMaisonContext". It's the one containing the generated table...

I would like to know where this name come from and how can I do so all the generated table goes into my connectionstring only with the name I specified. I just want one database. I'm sure it's just one tag somewhere in the context but I can't find this information on google.

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


preguntado el 28 de agosto de 12 a las 12:08 - just do Database.SetInitializer<YourDataContext>(null); this will override that behaviour -

Thanks, I will try that this evening. Can you write a real answer instead of a comment so I can give you the point for a good answer? -

2 Respuestas

It's because the connectionString that you have didnt find the file for your database so CodeFirst create one automaticaly by using the full namespace of where your DbContext is for the filename.

Insert this text in the connectionString by changing the filename.mdf by the filename that you want: AttachDBFilename=|DataDirectory|filename.mdf. The database file will be in your project\App_Data carpeta.

tendrás algo como esto:

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

Be sure that your DbContext is using the right connectionString by passing the connectionStringName in the DbContext constructor like this:

public class RecettesMaisonContext : DbContext
    public RecettesMaisonContext()
        : base("RecettesMaison")

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        var typesToRegister =
                type =>
                type.BaseType.IsGenericType &&
                type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));

        foreach (object configurationInstance in typesToRegister.Select(Activator.CreateInstance))


Respondido 29 ago 12, 14:08

There is nothing in my App_Data folder since my database is hosted in SQL Server Express that came with Management Studio (not in visual studio). Any other idea? - Jean-François Côté

can you confirm that your DbContext is using the right connectionString? Tell me if you pass the connectionStringName in the constructor of your DbContext. If not, do it there. - Alejandro Jobín

Do you have any example on how to do this because right now, my DBContext doesn't have a constructor. It only have an override of OnModelCreating - Jean-François Côté

Thanks! But now when I do a SaveChange(), I get an exception (DbUpdateException) like if there were no tables... I'm about to give up :( - Jean-François Côté

It's because your database is already created and CodeFirst don't know what to add or update in your schema. If you can, delete your database and let CodeFirst create it by itself. Or, add manualy the tables to your database. - Alejandro Jobín

By default, code first will create a DB for you. If you however want to override this behaviour just use



Respondido 28 ago 12, 13:08

This doesn't work. I want my database to be created or dropped but with the name specified in the connection string. I think it's just normal because the user on the production server can't create a database but it can use an existing database. Also, the "automatic" name is not interesting at all. Any other idea? - Jean-François Côté

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.