这是我到目前为止:
<add name="gymEntities1" connectionString="metadata=res://*/DateModel.csdl|res://*/DateModel.ssdl|res://*/DateModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=gym;user id=sa;password=xxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
它适用于我的LocalHost数据库,我可以从中加载我的数据.但是,我有一个服务器,安装sqlserver与我的数据库,基本上,当我改变我的sqlcommands连接字符串这项工作但在我的程序的一些部分我使用实体框架,不知道如何更改它连接字符串,与一些帖子在stackoverflow中我改变它
<add name="gymEntities2" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:46.105.124.144;initial catalog = gym ;User ID=sa;Password=xxxx"" providerName="System.Data.EntityClient" />
Run Code Online (Sandbox Code Playgroud)
但它仍然从我的localhost读取数据,而不是连接到我的服务器.我不知道当我将这个连接字符串更改为我的服务器时,它仍然从我的localhost数据库中读取数据.
从App.Config更改连接字符串的最佳方法是什么?
小智 3
由于其他人向您建议,这种可能性较小。但是,您可能在 web.config 或 app.config 之一中缺少连接字符串。将字符串复制到每个项目是一个好习惯。例子。我的解决方案中有 3 个不同的项目(Library、WCF、WPF)。我将以下连接字符串复制到每个项目(一个示例用于本地 SQL Server,另一个示例用于 Azure):
<connectionStrings>
<add name="LocalSQLServerSample.CodeREDEntities" connectionString="metadata=res://*/CodeRED.csdl|res://*/CodeRED.ssdl|res://*/CodeRED.msl;provider=System.Data.SqlClient;provider connection string="data source=MachineName\ServerName;initial catalog=CodeRED;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="AzureSQLServerSample.CodeREDEntities" connectionString="metadata=res://*/CodeRED.csdl|res://*/CodeRED.ssdl|res://*/CodeRED.msl;provider=System.Data.SqlClient;provider connection string='data source=azureservername.database.windows.net;initial catalog="CodeRED";persist security info=True;user id=CodeRED;password=R%Chd$g*VHs28eEr;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)
您已经提到您正在使用实体框架。您是否使用 ObjectContext 来访问它?如果是,我有以下方法可以在每次要访问任何数据库时调用它:
从上面的示例:name="LocalSQLServerSample.CodeREDEntities"
_containerName是 CodeREDEntities (对于我的所有连接来说都是相同的)。 环境是确定您要连接到哪个数据库。例如,在上面的连接示例中,我有LocalSQLServerSample和AzureSQLServerSample,并且我通常有诸如生产、开发、测试...之类的内容。
public static ObjectContext getObjectContext(string environment, bool isReadOnly)
{
environment = environment == null ? "" : environment.Trim();
environment = environment.Length == 0 ? "" : (environment + ".");
ObjectContext objectContext = new ObjectContext(
ConfigurationManager.ConnectionStrings[environment + _containerName].ToString());
objectContext.DefaultContainerName = _containerName;
objectContext.CommandTimeout = 0;
objectContext.ContextOptions.ProxyCreationEnabled = !isReadOnly;
return objectContext;
}
Run Code Online (Sandbox Code Playgroud)
如何使用它的示例:
Common是一个公共类,我用它来存储共享信息,例如获取用于 Common.getInnerExceptionMessage 的公共错误格式。
另外,您不必总是传递环境,您可以将其存储为常量以便能够调用它,例如(当我需要特定调用时,我总是传递它以便能够混合连接):您可以修改如果您不希望将其传递到任何地方,可以通过更改_selectedEnvironment从任何地方进行连接。
public const string _ENVIRONMENT_DEVELOPMENT = "LocalSQLServerSample";
public const string _ENVIRONMENT_PRODUCTION = "AzureSQLServerSample";
public static string _selectedEnvironment = _ENVIRONMENT_PRODUCTION;
Run Code Online (Sandbox Code Playgroud)
根据id获取item的示例:
注意:User是实体框架从数据库生成的类。
public UsersDataGrid GetItem(string environment, long id)
{
ObjectContext objectContext = Common.getObjectContext(environment, false);
try
{
var item = objectContext.CreateObjectSet<User>()
.Where(W => W.ID == id)
.Select(S => new UsersDataGrid()
{
Active = S.Active,
ID = S.ID,
Unique_ID = S.Unique_ID,
First_Name = S.First_Name.ToUpper(),
Last_Name = S.Last_Name.ToUpper(),
Email = S.Email,
School = S.School.Title.ToUpper(),
Gender = S.Gender.Title.ToUpper(),
TShirt_Size = S.TShirt_Size.Title.ToUpper(),
GUID = S.GUID + "",
Note = S.Note,
Machine_User = S.Machine_User,
Machine_Name = S.Machine_Name,
Created_On = S.Created_On,
Last_Updated_On = S.Updated_On
}).FirstOrDefault();
return item;
}
catch (Exception exception)
{
return new UsersDataGrid()
{
Note = ("Service Error: " +
Common.getInnerExceptionMessage(exception))
};
}
}
Run Code Online (Sandbox Code Playgroud)
第二个示例:更新用户:
注意:Common.CopyValuesFromSourceToDestinationForUpdate只是将项目从项目对象复制到实体项目的通用方法,您可以正常复制值,例如entityItem.ID = item.ID等等...
public Result Update(string environment, User item)
{
ObjectContext objectContext = WCF_Service_Library.Classes.Common.getObjectContext(environment, false);
try
{
var entityItem = objectContext.CreateObjectSet<User>()
.AsEnumerable().Where(Item => Item.ID == item.ID).ToList().FirstOrDefault();
if (entityItem == null)
return new Result("Item does NOT exist in the database!");
entityItem = Common.CopyValuesFromSourceToDestinationForUpdate(item, entityItem) as User;
objectContext.SaveChanges();
return new Result(entityItem.ID);
}
catch (Exception exception)
{
return new Result("Service Error: " + Common.getInnerExceptionMessage(exception));
}
}
Run Code Online (Sandbox Code Playgroud)
如果您发布应用程序并仅签署 WPF 项目,则在发布过程中不会出现错误,但可能无法连接到数据库。您必须签署解决方案中的所有项目。
希望这可以帮助您解决问题
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |