none
Having trouble compiling code-first EF4 class in runtime

    Question

  • 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.ReferencedAssemblies.Add("mscorlib.dll"); 
                compilerparams.ReferencedAssemblies.Add("System.dll");
                compilerparams.ReferencedAssemblies.Add("System.Core.dll"); 
                compilerparams.ReferencedAssemblies.Add("System.Xml.dll");
                compilerparams.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");

    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:

    {Compiler Errors :
    
    Line 13,10	: The type or namespace name 'Column' could not be found (are you missing a using directive or an assembly reference?)
    
    Line 13,10	: The type or namespace name 'ColumnAttribute' could not be found (are you missing a using directive or an assembly reference?)
    
    Line 15,10	: The type or namespace name 'DatabaseGenerated' could not be found (are you missing a using directive or an assembly reference?)
    
    Line 15,10	: The type or namespace name 'DatabaseGeneratedAttribute' could not be found (are you missing a using directive or an assembly reference?)
    
    Line 9,6	: The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?)
    
    Line 9,6	: The type or namespace name 'TableAttribute' could not be found (are you missing a using directive or an assembly reference?)}



    Any help?

    Thank you.



    -- Hamid Eghbalz Technical Manager Douran Co




    • Edited by ano0sh Friday, April 27, 2012 3:36 PM
    Friday, April 27, 2012 2:43 PM

Answers

All replies

  • On 4/27/2012 10:43 AM, ano0sh wrote:
    > 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.
    >
    >
    > ------------------------------------------------------------------------
    >
    > -- Hamid Eghbalz Technical Manager Douran Co
    >
     
    I would suspect that you are going to have to set reference to
    System.Linq in a config file that this second part uses and it's not there.
     
     
    Friday, April 27, 2012 5:08 PM

  • I added

     compilerparams.ReferencedAssemblies.Add("System.Linq");
    to BuildAssembly method and get this exception:


    {error CS0006: Metadata file 'System.Linq' could not be found}


    -- Technical Manager

    Friday, April 27, 2012 5:13 PM
  • On 4/27/2012 1:13 PM, ano0sh wrote:
    >
    > I added
    >
    >   compilerparams.ReferencedAssemblies.Add("System.Linq");
    >
    > to BuildAssembly method and get this exception:
    >
    >
    > {error CS0006: Metadata file 'System.Linq' could not be found}
     
    Well I suspect that you are going to have to copy the System.Linq DLL to
    where the exe is located so that it can be found. That's what happens
    when you reference a .NET DLL. The DLL is copied to the Bin directory
    where the exe is located so that .NET can find it for the application to
    use, as an example.
     
    Friday, April 27, 2012 5:20 PM
  • is this necessary when i use .net ?

    i have .net 4 installed on my machine.

    .net dlls never copy to bin folder. am i right?

     

    -- Technical Manager

    Friday, April 27, 2012 5:26 PM
  • On 4/27/2012 1:26 PM, ano0sh wrote:
    > is this necessary when i use .net ?
     
    Well any .NET DLL is never registered with the registry. But it must be
    found by a .NET application. The last I remember, a .NET DLL has to be
    in the location of the executable in order for it to be found. This may
    only apply to classlib projects within a solution.
    >
    > i have .net 4 installed on my machine.
    >
    > .net dlls never copy to bin folder. am i right?
     
    I don't know how .NET Framework 4.0 handles it now. Maybe the DLL needs
    to be in the GAC so .NET can find it.
     
    Friday, April 27, 2012 7:16 PM
  • Hi guy,

      Please read this blog article talking about hwo to process Code-First Development with Entity Framework 4.

       http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, May 01, 2012 6:03 AM
    Moderator