简体   繁体   English

将代码优先实体框架指向创建的数据库

[英]Pointing a Code First Entity Framework to a created database

I am creating a Code First Entity Framework to create a database to where I point it to. 我正在创建一个Code First Entity Framework,以创建一个指向我指向的数据库。 I have an instance of SQL => DESKTOP-98TG6JE\\SQLEXPRESS and another one => DESKTOP-98TG6JE 我有一个SQL实例=> DESKTOP-98TG6JE\\SQLEXPRESS和另一个实例=> DESKTOP-98TG6JE

In my Asp MVC code, my connection string is like so in the Web.config 在我的Asp MVC代码中,我的连接字符串就像在Web.config

<connectionStrings>
    <add name="MusicStoreDB" connectionString="Data Source=DESKTOP-98TG6JE;Initial Catalog=MusicStoreDB;User ID=sa;Password=myPassword123" providerName="System.Data.SqlClient" />
</connectionStrings>

and in my MusicStoreDB : DbContext , 在我的MusicStoreDB : DbContext

public MusicStoreDB() : base("MusicStoreDB")
{
}

But when I run the program, it creates a database in the DESKTOP-98TG6JE\\SQLEXPRESS named DefaultConnection and on the instance where I want to create the database, no MusicStoreDB database is created. 但是,当我运行该程序时,它将在DESKTOP-98TG6JE\\SQLEXPRESS创建一个名为DefaultConnection的数据库,并且在要创建该数据库的实例上,没有创建MusicStoreDB数据库。 Image of the created database on the wrong instance 错误实例上创建的数据库的图像

Can you please explain it to me why this is happening? 您能告诉我为什么会这样吗? Thank you. 谢谢。

Check you connection string name provided in ApplicationDbContext : 检查您在ApplicationDbContext提供的连接字符串名称:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("MusicStoreDB")
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM