如何从另一个项目引用 APP_Data

发布于 2024-07-10 18:42:06 字数 1041 浏览 3 评论 0原文

我有以下项目解决方案:

  • 有一个 ASP.NET MVC Web 我有 SQL 的应用程序 App_Data 目录中的数据库(mdf 文件)。

  • 有一个类库,我在那里 有一些迁移来自 migratordotnet。 在我的构建文件中我 必须指定 SQL 的位置 数据库是这样它可以运行 迁移。

目前,在迁移项目中,我使用硬编码路径来访问连接字符串中的 SQL 数据库。 它看起来有点像这样:

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=C:\MySolution\MyMVCProject\App_Data\MyDatabase.mdf"

这不是我想要做的。 我也无法使用相对路径(例如 ..\MyMVCPProject\AppData\MyDatabase.mdf 因为 migratordotnet 中使用的 SQL 类无法正确翻译它)但我想使用某种它的替代路径,类似于您在 Web 项目的 Web.Config 中使用 |DataDirectory| 找到的内容,如下所示:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True"

我如何实现此目的? 我无法在迁移项目中使用该连接字符串。 因为|DataDirectory|将转到.NET Framework安装路径并在那里查找数据库文件。

I have the following project solution:

  • There is a ASP.NET MVC Web
    Application where I have a SQL
    database (mdf file) in the App_Data directory.

  • There is a class library where I
    have some migrations from
    migratordotnet. In my build file I
    have to specify where the SQL
    database is so it can run the
    migrations.

For the moment on the migration project I'm using a hardcoded path to access the SQL database in the connection string. It looks sort of like this:

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=C:\MySolution\MyMVCProject\App_Data\MyDatabase.mdf"

This is not how I want to do. I also cannot use relative paths (such as ..\MyMVCPProject\AppData\MyDatabase.mdf because the SQL classes used in migratordotnet won't be able to translate it right) But I want to use some kind of a substitute path to it, sort of what you'll find with |DataDirectory| in the Web.Config in the web project like this:

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;User Instance=True"

How do I do achieve this? I can't use that connection string in the migration project. Because the |DataDirectory| will go to .NET Framework installation path and look for the database file there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-07-17 18:42:06

您想要这样的东西(VB .NET 代码):

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=" & Server.MapPath("~\App_Data\MyDatabase.mdf")

Server.MapPath() 将在运行时将您的相对路径转换为完全限定的路径。

You want something like this (VB .NET code):

connectionString="Data Source=.\SQLExpress; Integrated Security=true; AttachDbFilename=" & Server.MapPath("~\App_Data\MyDatabase.mdf")

Server.MapPath() will turn your relative path into a fully qualified path at runtime.

绾颜 2024-07-17 18:42:06

您是否考虑过在 SQL Express 中永久附加数据库? 这样您就可以在两个项目中使用相同的连接字符串,并且都不会绑定到文件系统路径。

Have you considered just attaching the database permanently in SQL Express? That way you can use identical connection strings in both projects and neither will be tied to a file system path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文