我想要实现的是从我生成的c#类动态生成项目。 这班的内容进行了代码的第一代码生成实体的类似内容framework.The内容如下所示:当从文件编译汇编时发现命名空间错误
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的项目中引用生成的项目。
主要错误在编译时是:
类型或命名空间名称KeyAttribute'不存在命名空间中存在“System.ComponentModel.DataAnnotations”
(是否缺少程序集引用?)无法找到类型或名称空间'Key'
无法找到类型或名称空间'Table'。
我使用“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);
非常感谢帮助。
btw,在哪个目录下创建了程序集?我是否在'OutputAssembly'参数中指定它?谢谢。 –