问题描述
大家好,
我在Visual Studio 2017中首次创建ASP.NET CORE 1.1代码,
我为数据库创建了我的模型类和AppContext类,但是我的数据库仅在我进行迁移时创建,
当尝试添加任何数据时出现错误数据库:
System.Data.SqlClient.SqlException:'无法打开登录请求的数据库Mydatabase。登录失败。
用户'ServerName-3FT4ANB \ MyName'登录失败。'
但如果我制作迁移首先这个错误不会发生?
我尝试过:
我搜索但找不到我需要理解的这一点
Hello guys,
I created ASP.NET CORE 1.1 code first Project in Visual Studio 2017,
I made my model classes and AppContext class for Database, but my Database created only when i made Migration,
that error appear when try to add any data to database:
System.Data.SqlClient.SqlException: 'Cannot open database "Mydatabase" requested by the login. The login failed.
Login failed for user 'ServerName-3FT4ANB\MyName'.'
But if i make migration first this error not happen ?
What I have tried:
I search but cannot find what i need to understand this point
推荐答案
我为数据库创建了我的模型类和AppContext类,但是我的数据库只在我制作时才创建迁移,
I made my model classes and AppContext class for Database, but my Database created only when i made Migration,
当你对数据库采取行动时,会创建数据库。例如,简单地运行您的应用程序将不会创建数据库,但如果您将用户注册到数据库(或插入用户),则架构将更新。
Your database gets created when you take an action against the DB. For example, simply running your application won't create the databse, but if you go and register a user to the database (or insert a user) then the schema will be updated.
但是我认为数据库是在我运行我的应用程序时创建的,每次我运行它都是用任何更新创建的应用程序我在模型类中制作
我对类模型所做的任何更改都会反映在数据库中,例如将新属性添加到添加新列的特定类时数据库中的表
But I thought that the Database is created when i run my application and every time i run application it created with any update i make in the model classes
and any change i make to class model will reflect on the database, as example when add new property to specific class that add new column in the table in the database
您的数据库不仅知道根据代码更新自己,您要么拥有运行命令Update-Database( [])或者看起来您可以设置web.config来自动更新数据库( [])但我认为这是一个可怕的想法,因为你要进行数据库更改,未经测试,可能会破坏你的应用程序。
Your database doesn't just know to update itself based on code, you either have to run the command Update-Database (Entity Framework Code First Migrations[^]) or it looks like you can setup your web.config to automatically update the database (c# - Entity framework code first - how to run Update-Database for production database - Stack Overflow[^]) but I would think this is a terrible idea as you'd be committing DB changes that, without being tested, could break your app.
但是最终用户在生产级别使用应用程序呢?
But what about using application in Production level by end users ?
不确定你在这里问的是什么,但是首先使用代码对生产来说很好。只需创建一个部署流程,筛选任何与生产相关的迁移(这是我建议不要通过web.config自动更新数据库),这可能会破坏您的环境。
Not sure what you are asking here, but using code first is fine for production. Just create a deployment process that screens any production related migrations (this is where i would advise against auto updating DB via web.config) that could break your environment.
这篇关于在Visual Studio 2017中首次在ASP.NET CORE 1.1代码中创建数据库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!