简体   繁体   中英

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.

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. Most likely you need to add the necessary assembly references like you would add in a normal VS C# project. Try adding them in your BuildAssembly method using something like:

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; 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. Remember to remove NUGET components and reinstall in new NET target new version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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