gpt4 book ai didi

c# - 带有 SqlServerCe.3.5 的 Entity Framework - 连接异常

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:57 24 4
gpt4 key购买 nike

我对 SqlServerCe.3.5 连接的 Entity Framework 有问题。我有一个名为 Assets 的小项目,带有 .SDF 数据库和实体(模型名称是 Main)。现在,当我尝试连接到实体时,发生了一些奇怪的事情。第一次一切正常,但现在我必须添加这个:

 if (edmConnection.State != ConnectionState.Open)
{
edmConnection.Open();
}

因为与实体的连接一直处于关闭状态。添加此行后,我可以访问数据库和实体,但我收到此消息:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

这是堆栈跟踪:

at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) at System.Data.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString) at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) at BL.Model.DBEntities..ctor() in C:\Users\Orel\Documents\Visual Studio 2010\Projects\Assets\BL\Model\Main.Designer.cs:line 34 at BL.Handlers.mModelHandler..ctor() at BL.Handlers.mModelHandler.GetOnlyInstance() in C:\Users\Orel\Documents\Visual Studio 2010\Projects\Assets\BL\Handlers\mModelHandler.cs:line 30

这是我的代码和 app.config:

   private static mModelHandler _mInstance = null;
public static DBEntities m_context = null;


public static mModelHandler GetOnlyInstance()
{
if (_mInstance == null)
{
try
{
m_context = new DBEntities(GetConnectionString());
_mInstance = new mModelHandler();
}
catch (Exception)
{


throw;
}

}
return _mInstance;
}

public static EntityConnection GetConnectionString()
{
try
{
var filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if (filePath == null) throw new ArgumentNullException("filePath");

if (filePath.EndsWith("\\Assets\\bin\\Debug"))
{
filePath = filePath.Replace("\\Assets\\bin\\Debug", "\\BL\\DB.sdf");
}
var sqlCeConnectionString = string.Format("Data Source={0}", filePath);

// Create an EDM connection
EntityConnectionStringBuilder entity = new EntityConnectionStringBuilder();

entity.Metadata = "res://*/Model.Main.csdl|res://*/Model.Main.ssdl|res://*/Model.Main.msl";
entity.Provider = "System.Data.SqlServerCe.3.5";
entity.ProviderConnectionString = sqlCeConnectionString;

var edmConnectionString = entity.ToString();
var edmConnection = new EntityConnection(edmConnectionString);
if (edmConnection.State != ConnectionState.Open)
{
edmConnection.Open();
}
return edmConnection;
}
catch (Exception e)
{

throw;
}
}

应用程序配置:

<add name="DBEntities" connectionString="metadata=res://*/Model.Main.csdl|res://*/Model.Main.ssdl|res://*/Model.Main.msl;provider=System.Data.SqlServerCe.3.5;provider connection string='Data Source=|DataDirectory|\DB.sdf'" providerName="System.Data.EntityClient" />

我在 fourm 中读到问题可能是 app.config 和连接字符串不匹配,我也试过了但它不起作用..

我刚刚添加了一张这样的图片: Link

帮助!

奥廖尔

最佳答案

我认为您的连接字符串没有被正确解析。尝试使用 " 代替 '

我还认为,对于 SqlCE,引号内的 provider 应该是 provider=System.Data.SqlServerCe,3.5; 而不是 provider=System .Data.SqlServerCe.3.5;.

注意 , 就在版本 3.5 之前。

综合起来,我们有:

<add name="DBEntities"
connectionString="metadata=res://*/Model.Main.csdl|res://*/Model.Main.ssdl|res://*/Model.Main.msl;provider=System.Data.SqlServerCe,3.5;provider connection string= &quot;Data Source=|DataDirectory|\DB.sdf&quot;"
providerName="System.Data.EntityClient" />

希望这对您有所帮助。

关于c# - 带有 SqlServerCe.3.5 的 Entity Framework - 连接异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778134/

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