简体   繁体   English

在运行时无法编译代码优先EF4类:)

[英]Having trouble compiling code-first EF4 class in runtime :)

I'm try to compile this code at run time. 我正在尝试在运行时编译此代码。 this code is a code-first EF4 class. 此代码是代码优先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; }
    }
}

using this method that compile the given code. 使用此方法编译给定的代码。 I tested this code with a simple class. 我用一个简单的类测试了此代码。 it works. 有用。 but with given class, it doesn't work at all. 但是对于给定的类,它根本不起作用。

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;
            }
        }

and i'm getting some exceptions like this: 而且我得到了一些这样的例外:

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

Any help? 有什么帮助吗?

Thank you. 谢谢。

Seems like you don't have in 'compilerparams' ReferencedAssemblies set up. 似乎您没有在“ compilerparams”中建立ReferencedAssemblies。 Most likely you need to add the necessary assembly references like you would add in a normal VS C# project. 您很有可能需要添加必要的程序集引用,就像在普通的VS C#项目中添加一样。 Try adding them in your BuildAssembly method using something like: 尝试使用类似的方法将它们添加到您的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");

Besides this, make sure you have the following namespace usings in the code that you want to compile: using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; 除此之外,请确保要编译的代码中具有以下名称空间用法: using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; This should solve your compilation problems regarding this matter. 这应该可以解决有关此问题的编译问题。

I also met this problem, and upgrade website from NET 4.0 to NET 4.6.1, it's fixed. 我也遇到了这个问题,并将网站从NET 4.0升级到NET 4.6.1,此问题已修复。 Remember to remove NUGET components and reinstall in new NET target new version. 请记住要删除NUGET组件,然后在新的NET目标新版本中重新安装。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM