Dab*_*lin 4 c# sql-server sql-server-express localdb
所以..我几乎完成了所有的问题.但现在我处理另一个问题.我使用了这个连接字符串:
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database=Database1.mdf");
Run Code Online (Sandbox Code Playgroud)
它会抛出下一个错误:
用户登录失败'
如果我删除.mdf扩展名,它会抛出相同的错误
现在,如果我将以下内容添加到字符串:
Integrated Security = true
Run Code Online (Sandbox Code Playgroud)
它抛出这个:
无法打开登录请求的数据库"Database1.mdf".登录失败.用户'rBcollo-PC\rBcollo'登录失败.
PS for Integrated Security = false它会抛出"登录失败的用户"
问题是我没有使用数据库的任何用户名或密码.
有什么帮助吗?
注意:如果您使用EntityFramework Core,我的回答将适用SQL Server
此错误可能是因为' Database does not exist'和应用程序尝试执行数据库操作的原因.
在执行任何数据库操作之前,您只需使用以下行.
_context.Database.EnsureCreated();
Run Code Online (Sandbox Code Playgroud)
什么是EnsureCreated?
// Summary:
// Ensures that the database for the context exists. If it exists, no action is
// taken. If it does not exist then the database and all its schema are created.
// If the database exists, then no effort is made to ensure it is compatible with
// the model for this context.
// Note that this API does not use migrations to create the database. In addition,
// the database that is created cannot be later updated using migrations. If you
// are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate()
// method to ensure the database is created and all migrations are applied.
//
// Returns:
// True if the database is created, false if it already existed.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18364 次 |
| 最近记录: |