Second database created with MVC3, how to prevent?

╄→гoц情女王★ 提交于 2019-12-22 08:59:23

问题


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!


回答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);
        }
    }

    ...
}



回答2:


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



来源:https://stackoverflow.com/questions/12158114/second-database-created-with-mvc3-how-to-prevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!