第一个可能的问题:
不太可能,因为其他人向您建议。但是,您的 web.config 或 app.config 之一中可能缺少连接字符串。
将字符串复制到每个项目中是一个好习惯。例子。我的解决方案中有 3 个不同的项目(库、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>
第二个可能的问题:
您提到您正在使用实体框架。您是否使用 ObjectContext 来访问它?如果是的话,每次我想访问任何数据库时,我都有下面的方法来调用它:
来自上面的示例:name="LocalSQLServerSample.CodeREDEntities"
_containerName 是 CodeREDEntities(我的所有连接都相同)。
环境 用于确定您要连接到哪个数据库。例如,在上面的连接示例中,我有 LocalSQLServerSample 和 AzureSQLServerSample,我通常有 PRODUCTION、DEVELOPMENT 之类的东西>, 测试....
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;
}
使用方法示例:
Common 是我用来存储共享信息的通用类,例如获取用于 Common.getInnerExceptionMessage 的常见错误格式。
另外,您不必总是传递环境,您可以将其存储为常量以便能够调用它,例如(我总是传递它以便在需要特定调用时能够混合连接):如果您不想在任何地方传递连接,可以通过更改 _selectedEnvironment 从任何地方修改连接。
public const string _ENVIRONMENT_DEVELOPMENT = "LocalSQLServerSample";
public const string _ENVIRONMENT_PRODUCTION = "AzureSQLServerSample";
public static string _selectedEnvironment = _ENVIRONMENT_PRODUCTION;
根据 id 获取商品的示例:
注意: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))
};
}
}
第二个示例:更新用户:
注意:Common.CopyValuesFromSourceToDestinationForUpdate 只是将项目从 item 对象复制到 entityItem 的通用方法,您可以正常复制值,例如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));
}
}
第三个问题(看起来不像,但你可能会遇到):
如果您发布您的应用程序并且只签署您的 WPF 项目,您在发布过程中不会出错,但您可能无法连接到数据库。您必须签署解决方案中的所有项目。
希望这能帮助您解决问题