4

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

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

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)

Thanks!

2

2 Answers 2

1

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 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>

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 =
            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);
        }
    }

    ...
}
7
  • 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? Aug 28, 2012 at 22:56
  • 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. Aug 29, 2012 at 1:13
  • 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 Aug 29, 2012 at 2:19
  • 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 :( Aug 29, 2012 at 22:20
  • 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. Aug 29, 2012 at 22:23
1

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

Database.SetInitializer<YourDataContext>(null);

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

1
  • 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? Aug 28, 2012 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.