attachdbfilename的值无效(Invalid value for attachdbfilename)

编程入门 行业动态 更新时间:2024-10-28 13:27:18
attachdbfilename的值无效(Invalid value for attachdbfilename)

我目前正在尝试分发依赖.NET中某种形式的数据库的应用程序。 我正在尝试使用SQL Server Express或SQL Server Express LocalDB。

为了访问数据库,我正在使用实体框架。

现在的目标是创建一个可部署的版本,不需要手动对SQL服务器进行任何更改。 它需要在启动应用程序时根据需要进行安装和设置。 在我的开发计算机上一切顺利。 一旦部署到另一台计算机,问题就开始了。

我尝试了LocalDB,以避免在目标机器上手动设置数据库结构的要求。 然而,在启动应用程序时,它报告连接字符串的问题。 attachdbfilename无效。

这个文件名设置为attachdbfilename=|DataDirectory|\database\Db.mdf正如我所说。 在我的开发机器上这是有效的。 在使用已部署的文件和从IDE运行时都是如此。 然而,目标mashine报告了这个问题。 我使用ClickOnce和我的应用程序安装了SQL Server Express 2012 LocalDB。 部署的文件包含正确目录中的mdf文件。

完整的连接字符串是:

<connectionStrings> <add name="DbContainer" connectionString="metadata=res://*/database.DbModel.csdl|res://*/database.DbModel.ssdl|res://*/database.DbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\database\Db.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

我没有看到attachdbfilename无效的原因。 在目标机器上安装sql server似乎没问题。

我也尝试使用普通的SQL Server 2012 Express来实现它。 我怎么想如果我想使用它,没有附加的数据库文件我需要手动创建所需的数据库。 这不是一种选择。

我在一些问题中读到这表明create ObjectContext存在问题,但这个问题看起来还不错:

Public Partial Class DbContainer Inherits ObjectContext Public Sub New() MyBase.New("name=DbContainer", "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub Public Sub New(ByVal connectionString As String) MyBase.New(connectionString, "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub Public Sub New(ByVal connection As EntityConnection) MyBase.New(connection, "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub ...

就像它是由实体框架生成的一样。

关于如何解决这个问题,我的想法已经不多了。

I'm currently trying to distribute a application that relies on some form of database in .NET. I am trying to use SQL Server Express or SQL Server Express LocalDB.

For accessing the database I am using the entity framework.

Now the target is to create a deployable version that does not require any alterations on the SQL server by hand. It needs to install and setup as far as needed to launch the application. All goes well on my development computer. How ever once deploying to another computer the problems begin.

I tried the LocalDB in order to avoid the requirement to setup the database structure by hand on the target machine. How ever upon launching the application its reports a problem with the connection string. The attachdbfilename is not valid.

This filename is set to attachdbfilename=|DataDirectory|\database\Db.mdf As I said. On my development maschine this works. Both when using the deployed files and when running it from the IDE. Yet the target mashine reports the problem. I installed the SQL Server Express 2012 LocalDB using ClickOnce along with my application. The deployed files contain the mdf file in the proper directory.

The full connection strings are:

<connectionStrings> <add name="DbContainer" connectionString="metadata=res://*/database.DbModel.csdl|res://*/database.DbModel.ssdl|res://*/database.DbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\database\Db.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings>

I fail to see a reason why the attachdbfilename is not valid. The installation of the sql server on the target machine seems to be okay.

I also tried to pulish it with the normal SQL Server 2012 express. How ever I assume if I want to use this, without a attached database file I need to create the required database by hand. And that is not a option.

I read in some questions that this indicates a problem with the create ObjectContext but this one is looking good as well:

Public Partial Class DbContainer Inherits ObjectContext Public Sub New() MyBase.New("name=DbContainer", "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub Public Sub New(ByVal connectionString As String) MyBase.New(connectionString, "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub Public Sub New(ByVal connection As EntityConnection) MyBase.New(connection, "DbContainer") MyBase.ContextOptions.LazyLoadingEnabled = true OnContextCreated() End Sub ...

Just like it got generated by the entity framework.

I am running out of ideas on how to solve this problem.

最满意答案

好的问题原来是|DataDirectory|

我使用我使用的正确的单击数据目录手动替换连接字符串中的此条目:

Dim dataDir As String If ApplicationDeployment.IsNetworkDeployed Then Dim ad = ApplicationDeployment.CurrentDeployment dataDir = ad.DataDirectory Else dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) End If

没有问题再加载数据库文件。 我不能说为什么要更换|DataDirectory| 不管用。 对于开发计算机上的版本,这只是替换为空字符串...这是有效的,因为工作目录是引用的数据库目录的父目录。

使用ClickOnce时,数据文件(包括数据库)位于不同的目录中。 这会导致连接字符串失败。 上面的代码修复了这个问题。 我无法解释为什么这不是开箱即用的。

Okay the problem turned out to be the |DataDirectory|

I replace this entry in the connection string by hand using the proper one click data directory I optained using:

Dim dataDir As String If ApplicationDeployment.IsNetworkDeployed Then Dim ad = ApplicationDeployment.CurrentDeployment dataDir = ad.DataDirectory Else dataDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) End If

No problem loading the database file anymore. I can't say why the replacement of the |DataDirectory| is not working. For the version on the development computer this is simply replace with a empty string... and this works because the working directory is the parent of the refered database directory.

How ever when using ClickOnce the data files, including the database, is located in a different directory. This causes the connect string to fail. The code above fixes this problem. I can't explain for what reason this is not working out of the box.

更多推荐

database,问题,SQL,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-07-29 18:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:attachdbfilename   Invalid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!