简体   繁体   English

如何从EF Configuration.cs文件读取文件内容?

[英]How to read a file content from EF Configuration.cs file?

I am using EF 6 Code-base migration. 我正在使用EF 6代码库迁移。 I want to read the file from Configuration.cs Seed() method to insert as default data. 我想从Configuration.cs Seed()方法中读取文件以作为默认数据插入。 I am using HostingEnvironment.MapPath() as below: 我正在使用HostingEnvironment.MapPath()如下:

string _homeContent = System.IO.File.ReadAllText( HostingEnvironment.MapPath(@"~/Data/DefaultContents/Home.html"));

But HostingEnvironment.MapPath return null value when I run the Update-database command from Package-Management Console . 但是,当我从Package-Management Console运行Update-database命令时,HostingEnvironment.MapPath返回null值。

Running Seed method. 运行种子方法。

System.ArgumentNullException: Value cannot be null. System.ArgumentNullException:值不能为null。

Parameter name: path at System.IO.File.ReadAllText(String path) 参数名称:System.IO.File.ReadAllText的路径(字符串路径)

Kindly advise how to read the physical file from Configuration.cs Seed() method. 请告知如何从Configuration.cs Seed()方法读取物理文件。

May I answer by myself. 我可以自己回答。

Server.MapPath and HostingEnvironment.MapPath is not available in this time because HttpContext object is not ready yet. 由于HttpContext对象尚未准备好,所以Server.MapPath和HostingEnvironment.MapPath暂时不可用。

AppDomain.CurrentDomain.BaseDirectory return "..\\your project directory\\bin" AppDomain.CurrentDomain.BaseDirectory返回“ .. \\您的项目目录\\ bin”

Eg C:\\YourSolutionDirectory\\YourProjectDirectory\\bin 例如C:\\ YourSolutionDirectory \\ YourProjectDirectory \\ bin

But we need to go up from " bin " folder, so that using Path.Combine() method and get exact file path. 但是我们需要从“ bin ”文件夹上 ,以便使用Path.Combine()方法并获取确切的文件路径。

string homeFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", @"Data\DefaultContents\Home.html");

Result: C:\\YourSolutionDirectory\\YourProjectDirectory\\Data\\DefaultContents\\Home.html 结果:C:\\ YourSolutionDirectory \\ YourProjectDirectory \\ Data \\ DefaultContents \\ Home.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未调用EF迁移configuration.cs中的种子方法 - Seed method in EF Migrations configuration.cs not being called Entity Framework Core Enable-Migrations 构建带有错误的 Configuration.cs 文件 - Entity Framework Core Enable-Migrations build the Configuration.cs file with Errors 如何在代码优先迁移中解决Configuration.cs中的错误 - How to solve error in Configuration.cs in code first migration 为什么我的所有模型类都被添加到我使用多个数据上下文类时创建的第一个Configuration.cs文件中? - Why do all of my model classes get added to the first Configuration.cs file that gets created when I'm using multiple Data Context classes? 在MVC5 Identity中将角色添加到Configuration.cs - Add roles into Configuration.cs in MVC5 Identity 实体框架代码优先:Configuration.cs种子或自定义初始化程序 - Entity Framework Code First: Configuration.cs seed or custom initializer 运行enable-migrations后,configuration.cs中出现错误 - Error in configuration.cs after enable-migrations run 使用EF6的配置文件中的WCF连接字符串 - WCF connectionstring from configuration file with EF6 MVC 4 DbContext Enable-Migrations重新编写Configuration.cs中的种子代码 - MVC 4 DbContext Enable-Migrations re-writes seed code in Configuration.cs 如何在配置文件中的EF中放置唯一的密钥约束? - How to put unique key constrain in EF ,in configuration file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM