【发布时间】:2016-04-22 13:59:48
【问题描述】:
我想要实现的是从我生成的 c# 类动态生成一个项目。 该类的内容类似于实体框架代码优先代码生成的内容。内容如下:
namespace ElasticTables
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.KeyAttribute;
[Table("address")]
public partial class address
{
[Key]
public decimal id { get; set; }
public string name { get; set; }
}
}
我从数据库中的表生成这些文件,然后尝试以编程方式编译它,以便我可以在另一个使用 API 的项目中引用生成的项目。
编译时的主要错误是:
命名空间“System.ComponentModel.DataAnnotations”中不存在类型或命名空间名称“KeyAttribute”(您是否缺少程序集引用?)
找不到类型或命名空间“键”
找不到类型或命名空间“表”。
我正在使用“CSharpCodeProvider”
var provider = new CSharpCodeProvider();
var options = new CompilerParameters
{
OutputAssembly = "ElasticTables.dll",
CompilerOptions = "/optimize"
};
我有以下引用的程序集
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.dll");
options.ReferencedAssemblies.Add(Directory.GetCurrentDirectory() + "\\EntityFramework.SqlServer.dll");
我有一个包含文件路径的字符串数组,称为源,我尝试使用以下行进行编译
CompilerResults results = provider.CompileAssemblyFromFile(options, sources);
非常感谢您的帮助。
【问题讨论】:
-
顺便说一句,程序集是在哪个目录中创建的?我是否在“OutputAssembly”参数中指定它?谢谢。
标签: c# entity-framework namespaces data-annotations csharpcodeprovider