【问题标题】:Having trouble compiling code-first EF4 class in runtime :)在运行时编译代码优先的 EF4 类时遇到问题 :)
【发布时间】:2012-04-27 14:33:55
【问题描述】:

我正在尝试在运行时编译此代码。此代码是代码优先的 EF4 类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace EFCodeFirst.Model.Models
{
    [Table("Blog")]
    public class Blog
    {
        public Guid Id { get; set; }
        [Column("txtTitle")]
        public string Title { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string ShortTitle { get { return Title; } }
        public string BloggerName { get; set; }
        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Post
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public DateTime DateCreated { get; set; }
        public string Content { get; set; }
        public Guid BlogId { get; set; }
    }
}

使用这个方法编译给定的代码。 我用一个简单的类测试了这段代码。有用。但是对于给定的课程,它根本不起作用。

private Assembly BuildAssembly(string code)
        {
            Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
            ICodeCompiler compiler = provider.CreateCompiler();
            CompilerParameters compilerparams = new CompilerParameters();
            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory = true;
            CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
            if (results.Errors.HasErrors)
            {
                StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
                foreach (CompilerError error in results.Errors)
                {
                    errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
                }
                throw new Exception(errors.ToString());
            }
            else
            {
                return results.CompiledAssembly;
            }
        }

我遇到了一些这样的异常:

error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)}

有什么帮助吗?

谢谢。

【问题讨论】:

    标签: c# c#-4.0 compilation runtime


    【解决方案1】:

    似乎您没有在“compilerparams”中设置 ReferencedAssemblies。 很可能您需要添加必要的程序集引用,就像在普通的 VS C# 项目中添加一样。尝试使用以下方式将它们添加到您的 BuildAssembly 方法中:

    compilerparams.ReferencedAssemblies.Add("mscorlib.dll"); compilerparams.ReferencedAssemblies.Add("System.dll"); compilerparams.ReferencedAssemblies.Add("System.Core.dll"); compilerparams.ReferencedAssemblies.Add("System.Xml.dll"); compilerparams.ReferencedAssemblies.Add("EntityFramework.dll");

    除此之外,请确保您在要编译的代码中使用以下命名空间:using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; 这应该可以解决您在此问题上的编译问题。

    【讨论】:

    • 我添加了这些代码 compilerparams.ReferencedAssemblies.Add("mscorlib.dll"); .Add("System.dll"); .Add("System.Core.dll"); .Add("System.Xml.dll"); .Add("System.ComponentModel.DataAnnotations.dll");并得到这些错误。找不到类型或命名空间名称“列”“”“”“ColumnAttribute”找不到“”“”“DatabaseGenerated”找不到“”“”“DatabaseGeneratedAttribute”找不到“”“”找不到 Table' """" 找不到 'TableAttribute' 似乎编译器不知道某些 EF 类。
    • 您需要将 EF 引用添加到您的编译器参数,例如:compilerparams.ReferencedAssemblies.Add("EntityFramework.dll") 并确保您在要编译的代码中使用以下命名空间: “使用 System.Data.Entity;使用 System.ComponentModel.DataAnnotations;”我更新了我的答案以包括这个。
    【解决方案2】:

    我也遇到了这个问题,把网站从NET 4.0升级到NET 4.6.1,解决了。请记住删除 NUGET 组件并在新的 NET 目标新版本中重新安装。

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 2018-09-23
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多