gpt4 book ai didi

asp.net-mvc - 在 MVC 4 中为实体动态创建数据库连接字符串

转载 作者:行者123 更新时间:2023-12-02 14:45:19 29 4
gpt4 key购买 nike

我在 MVC 4 项目中有一个实体,连接字符串由实体创建向导放置在 web.config 中。

我需要更改此设置并在运行时将密码分配给连接字符串(存储在 web.config 外部)。

  1. 如何将 web.config 外部的值与 web.config 中存储的字符串组合起来?

  2. 或者我可以将实体连接完全移到 web.config 之外吗?

这是现有的实体连接字符串:

add name="MyEntities" connectionString="metadata=res://*/Models.NewUsers.csdl|res://*/Models.NewUsers.ssdl|res://*/Models.NewUsers.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mydb;initial catalog=MyDatabase;persist security info=True;user id=sa;password=Mypassword;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

最佳答案

对于 2,您始终可以在创建新的连接字符串(不是来自 web.config)时直接将其传递到上下文:

string newCS = "add name=...";
var context = new MyEntities(newCS);

对于 1,use EntityConnectionStringBuilder解析现有的 CS 或构建新的 CS。

关于asp.net-mvc - 在 MVC 4 中为实体动态创建数据库连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13605923/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com