简体   繁体   English

我可以将SqlCredential与Entity Framework一起用于SQL连接吗?

[英]Can I use SqlCredential with Entity Framework for the SQL connection?

I would like to use the secure SqlCredential object to handle credentials for connecting to our SQL database. 我想使用安全的SqlCredential对象来处理用于连接到我们的SQL数据库的凭据。

.NET has an overload of the SqlConnection constructor that takes a connection string and SqlCredential so you don't have to embed the credentials in the connection string itself. .NET具有SqlConnection构造函数的重载,该构造函数采用连接字符串和SqlCredential,因此您不必在连接字符串本身中嵌入凭据。 I would like to use something similar for Entity Framework when creating a DBContext instance. 创建DBContext实例时,我想对Entity Framework使用类似的东西。

  1. I need to control the connection string that gets used by Entity Framework for the connection. 我需要控制实体框架用于连接的连接字符串。
  2. I would like to use the more secure way of handling credentials, using either SQLCredential, NetworkCredential, or SecureString and passing that to the DBContext to create the connection. 我想使用更安全的方式来处理凭据,使用SQLCredential,NetworkCredential或SecureString并将其传递给DBContext来创建连接。

Is there any way to do this? 有什么办法吗?

(As an aside, we can't use Windows integrated security as our database is not set up for that and I am not in a position to change that.) (顺便说一句,我们不能使用Windows集成安全性,因为我们没有为此设置数据库,而且我无权更改它。)

You can create a regular SqlConnection with SqlCredential like you're used to and then pass this to the constructor of the entity framework DbContext. 您可以像以前一样使用SqlCredential创建常规SqlConnection,然后将其传递给实体框架DbContext的构造函数。

The DbContext has this constructor: DbContext具有以下构造函数:

public DbContext(DbConnection existingConnection, bool contextOwnsConnection);

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

相关问题 如何在项目中使用多个实体框架连接字符串? - How can I use multiple entity framework connection strings in a project? 我可以像使用 LINQ 到 SQL 一样使用实体框架吗? - Can I use the Entity Framework like I use LINQ to SQL? 我可以避免Entity框架使用SQL_VARIANT来查询吗? - Can I avoid Entity framework use SQL_VARIANT to query? 我可以使用一个连接字符串在Entity Framework中使用具有相同表结构的多个数据库吗? - Can I use one connection string to use multiple databases with the same table structure in Entity Framework? 如何在实体框架中使用SQL“ and”语句? - How can I use the SQL “and” statement in Entity Framework? 如何在未知状态下使用Entity Framework数据库连接使用SqlBulkCopy - How can I use SqlBulkCopy from Entity Framework database connection in unknown state 实体框架获得SQL连接 - Entity Framework getting an sql connection 我可以在没有实体框架的情况下使用LINQ吗? - Can I use LINQ without Entity Framework? 如何关闭实体框架中的连接字符串对象 - How can I close connection string object in Entity Framework 如何在实体框架数据上下文中的属性名称中使用sql reserved关键字? - How can I use a sql reserved keyword in the name of a property in an entity framework data context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM