简体   繁体   English

无法使用实体框架代码优先使用凭据创建数据库?

[英]Cannot Create Database with credentials using Entity Framework Code-First?

I'm a bit new to code-first approach but I used Database first approach before using entity framework, now I want to work with code first instead and its kinda a habit.我对代码优先方法有点陌生,但在使用实体框架之前我使用了数据库优先方法,现在我想先使用代码,这有点习惯。

I started creating models and its all good, then enabled migrations and add-migration as well and all works well, but when I run Update-Database command I get an error.我开始创建模型并且一切都很好,然后启用了迁移和添加迁移,并且一切正常,但是当我运行 Update-Database 命令时出现错误。

Hence I want to create a database with code-first using credentials, after a lot of research I didn't find a solution and mostly tutorials use "Trusted Connection".因此,我想使用凭据创建代码优先的数据库,经过大量研究后,我没有找到解决方案,而且大多数教程都使用“受信任的连接”。

What I use我用什么

App.Config应用程序配置

  <connectionStrings>
    <add name="CodeFirst" connectionString="Server=.\SQLEXPRESS;Database=CodeFirst;User ID=anyuser;Password=anypassword;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

DbContext Class数据库上下文类

public class CodeFirstContext : DbContext
    {
        public CodeFirstContext() : base("name=CodeFirst")
        {

        }

        public DbSet<Product> Products { get; set; }

        public DbSet<Category> Categories { get; set; }

    }

The Error I get我得到的错误

Login failed for user 'anyuser'.

The question is I did something wrong ?问题是我做错了什么? or using credentials can't be done using code-first approach ?或者无法使用代码优先方法使用凭据?

Thanks in advance提前致谢

OP:操作:

I want to create a database with code-first using credentials ,我想使用凭据创建代码优先的数据库

...and: ...和:

after a lot of research I didn't find a solution and mostly tutorials use "Trusted Connection".经过大量研究,我没有找到解决方案,大多数教程使用“可信连接”。

Therein lies the problem with code-first approaches when deploying to a new database using a specific login - it's a chicken and egg because:这就是使用特定登录名部署到新数据库时代码优先方法的问题 - 这是鸡和蛋,因为:

  1. the database doesn't yet exist数据库还不存在
  2. the connection string is requesting an explicit login连接字符串请求显式登录
  3. the login does not yet exist in say SQL说 SQL 中尚不存在登录名
  4. because the database doesn't exist因为数据库不存在

OP's config: OP的配置:

<connectionStrings>
    <add name="CodeFirst" 
         connectionString="Server=.\SQLEXPRESS;Database=CodeFirst;User ID=anyuser;Password=anypassword;" 
      ... />
  </connectionStrings>

So what's the solution?那么有什么解决办法呢?

  1. Use Integrated Security: this allows you to create the database from scratch from code.使用集成安全性:这允许您从代码中从头开始创建数据库。 However any other logins you may need might have to be created later.但是,您可能需要稍后创建任何其他登录名。 However, DB-speaking, it's a bit of a bad practice because you should get into the habbit of having one account for deployment (with just enough rights for deployment) and another for general app access (and no rights for deployment)但是,就 DB 而言,这是一种不好的做法,因为您应该养成使用一个帐户进行部署(具有足够的部署权限)和另一个用于一般应用程序访问(没有部署权限)的习惯

  2. Create Login First: this allows you to use your specified login however you most likely will need to create a blank database first from SQL before you can create the login.首先创建登录名:这允许您使用指定的登录名,但是您很可能需要先从 SQL 创建一个空白数据库,然后才能创建登录名。 Code-first purists may not like this idea.代码至上的纯粹主义者可能不喜欢这个想法。

The latter does follow DACPAC best practices you should already be familiar with from your time in database-first.后者确实遵循 DACPAC 最佳实践,您应该从您在数据库优先的时代就已经熟悉。 Just like how a DACPAC shouldn't be deploying logins and adjusting security neither should code-first.就像 DACPAC 不应该部署登录和调整安全性一样,代码优先也不应该。 Afterall at the end of the day, databases don't care about whether you use code-first or database-first (under the hood, both probably employ some form of encasulated scripting) but your DB Admin might raise an eyebrow depending on how security is being used.毕竟,数据库不关心您是使用代码优先还是数据库优先(在幕后,两者都可能使用某种形式的封装脚本),但您的数据库管理员可能会根据安全性如何正在使用。

Are database-first approaches better?数据库优先的方法更好吗?

In the theme of don't do that, try this instead , code-first tries to do everything via code and whilst a noble pursuit perhaps isn't realistic.在“不要那样做,试试这个”的主题下,代码优先尝试通过代码来完成所有事情,而崇高的追求可能并不现实。 Some things like attempting to create logins perhaps isn't possible as mentioned above.如上所述,诸如尝试创建登录名之类的事情可能是不可能的。

Meanwhile database-first schema changes are carried out by deploying DACPACs from the database side.同时,通过从数据库端部署 DACPAC 来执行数据库优先模式更改。 Authentication is via the login used to get into SSMS (for example) in the first place.身份验证首先是通过用于进入 SSMS(例如)的登录名进行的。 DACPAC deployment doesn't require code, .NET or otherwise. DACPAC 部署不需要代码、.NET 或其他。

DACPACs can create and/modify database security including logins but is generally frowned upon. DACPAC 可以创建和/修改包括登录在内的数据库安全性,但通常不受欢迎。

You might want to re-think code-first.您可能想重新考虑代码优先。 Code-first and EF Migrations hasn't really succeeded as a concept in the real world where you have staged CD environments such as DEV; Code-first 和 EF Migrations 在现实世界中作为一个概念并没有真正成功,在那里你已经上演了 DEV 等 CD 环境; TEST;测试; UAT and PROD. UAT 和 PROD。

I see so many developers using code-first only for deploying to their local database .我看到很多开发人员使用code-first 仅用于部署到他们的本地数据库 Once there, they use different means to deploy the changes to other computers including TEST and PROD whether it's TSQL scripts or database backup.在那里,他们使用不同的方法将更改部署到其他计算机,包括TEST 和 PROD,无论是 TSQL 脚本还是数据库备份。

It just seems to be so much effort for something that doesn't even complete the race.对于甚至没有完成比赛的东西来说,这似乎是太多的努力。

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

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