代码优先迁移:从文件中读取 sql
我关注 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Server.MapPath 怎么样
http://msdn .microsoft.com/en-us/library/ms524632(v=vs.90).aspx
What about using Server.MapPath
http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
要直接从站点运行迁移,您可以添加此代码
要从任一进程(IIS 与程序包管理器控制台)运行迁移,您可以首先检查在使用 Server.MapPath 之前是否创建了服务器对象,这样您就可以识别是否“在 IIS 或程序包管理器控制台下。
如果服务器对象为空,则您位于程序包管理器控制台下,因此您可以使用更合适的内容来获取当前路径,例如
因此我会将此代码替换
为类似的代码
To run the migration directly from the site you could add this code
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
So I would replace this code
with something like this