Set credentials for Entity Framework in code, without using Integrated Security and only storing User ID in config file

落花浮王杯 提交于 2020-01-02 15:47:13

问题


So, I went to create an EDMX file today, and while going through the connection options the wizard asked if I wanted to "keep sensitive data out of the .config file and manually set it in code" this makes perfect sense to me (use my own encrypted "appsetting" for connection credentials).

Now, I'm sitting here trying to find out how to specify the credentials to my Entity Framework classes. The only thing I can find is the

db.Connection.ConnectionString 

property, but I'm not really sure what to do with it all it contains is name=MyDatabaseName.

TL;DR-- How does one create and use hard-coded connection for Entity Framework in C#? (I can un-hard code it once its in C#)


回答1:


If you cast the Connection object to an EntityConnection object, you can access the StoreConnection property which you can programmatically override with your hardcoded connectionstring.

See the example below:

EntityConnection entityConnection = (EntityConnection)context.Connection;
entityConnection.StoreConnection.ConnectionString = "YourConnectionString";


来源:https://stackoverflow.com/questions/6606020/set-credentials-for-entity-framework-in-code-without-using-integrated-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!