代码优先迁移:从文件中读取 sql

发布于 2024-12-12 21:33:47 字数 1029 浏览 0 评论 0原文

我关注 ADO.NET 团队 Code First Migrations 的帖子:Alpha 3 'No-Magic'演练

我正在尝试使用 sql 读取文件以在 asp.net 中进行迁移MVC 网络应用程序。我正在使用sql方法。问题是,无论我做什么,我都无法获得项目的根路径。我总是得到 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\'

有人能指出我打开文件的方法吗?

谢谢。

已编辑

公共分部类 AddMembership : DbMigration
{
    公共重写 void Up()
    {
        //var file = File.OpenText(System.IO.Path.GetDirectoryName() + "Model/Schema/membership-up.sql");
        //var file = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Model/Schema/membership-up.sql");
        //var 路径 = HttpContext.Current.Request.MapPath("/Models/Schema/membership-up.sql");
        var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
        Sql(文件.ReadToEnd());
    }

    公共重写 void Down()
    {
        // ....
    }
}

I following a post from ADO.NET team Code First Migrations: Alpha 3 ‘No-Magic’ Walkthrough

I'm trying to read a file with sql for a migration in a asp.net mvc web application. I'm using sql method. The problem is that no matter what I do I can not get a root path to my project. I always get 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\'

Could someone point me a way to open my file?

Thanks.

Edited

public partial class AddMembership : DbMigration
{
    public override void Up()
    {
        //var file = File.OpenText(System.IO.Path.GetDirectoryName() + "Model/Schema/membership-up.sql");
        //var file = File.OpenText(AppDomain.CurrentDomain.BaseDirectory + "Model/Schema/membership-up.sql");
        //var path = HttpContext.Current.Request.MapPath("/Models/Schema/membership-up.sql");
        var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
        Sql(file.ReadToEnd());
    }

    public override void Down()
    {
        // ....
    }
}

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

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

发布评论

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

评论(2

夏末染殇 2024-12-19 21:33:47

要直接从站点运行迁移,您可以添加此代码

var dbMigrator = new DbMigrator(new Settings());
dbMigrator.Update();

要从任一进程(IIS 与程序包管理器控制台)运行迁移,您可以首先检查在使用 Server.MapPath 之前是否创建了服务器对象,这样您就可以识别是否“在 IIS 或程序包管理器控制台下。
如果服务器对象为空,则您位于程序包管理器控制台下,因此您可以使用更合适的内容来获取当前路径,例如

Directory.GetCurrentDirectory();

因此我会将此代码替换

var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");

为类似的代码

String path;
if (HttpContext.Current.Server != null) {
    path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
} else {
    path = Path.Combine(Directory.GetCurrentDirectory(), "Models/Schema/membership-up.sql");
}

To run the migration directly from the site you could add this code

var dbMigrator = new DbMigrator(new Settings());
dbMigrator.Update();

To run migrations from either process (IIS vs Package Manager Console) you could check first if Server object is created before using Server.MapPath, this way you'd recognize if you're under IIS or under Package Manager Console.
If Server object is null you're under Package Manager Console so you could use something more appropriate to get your current path from like

Directory.GetCurrentDirectory();

So I would replace this code

var path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");

with something like this

String path;
if (HttpContext.Current.Server != null) {
    path = HttpContext.Current.Server.MapPath("/Models/Schema/membership-up.sql");
} else {
    path = Path.Combine(Directory.GetCurrentDirectory(), "Models/Schema/membership-up.sql");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文