Having trouble compiling code-first EF4 class in runtime

Discussion in 'Visual C# .Net' started by DN Admin, Apr 27, 2012.

  1. DN Admin

    Staff Member

    Country:
    United States
    Joined:
    Aug 7, 2010
    Location:
    Joliet, IL U.S.A.
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    <span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left I'm try to compile this code at run time. this code is a code-first EF4 class.</span>
    <pre class="prettyprint 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; }
    }
    }</pre>
    <br/>
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    <span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left 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.</span>
    <pre class="prettyprint private Assembly BuildAssembly(string code)
    {
    Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
    ICodeCompiler compiler = provider.CreateCompiler(); CompilerParameters compilerparams = new CompilerParameters(); compilerparams.ReferencedAssemblies.Add("mscorlib.dll"); <br/> compilerparams.ReferencedAssemblies.Add("System.dll");<br/> compilerparams.ReferencedAssemblies.Add("System.Core.dll"); <br/> compilerparams.ReferencedAssemblies.Add("System.Xml.dll");<br/> compilerparams.ReferencedAssemblies.Add("System.ComponentModel.DataAnnotations.dll");<br/>

    compilerparams.GenerateExecutable = false;
    compilerparams.GenerateInMemory = true;
    CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
    if (results.Errors.HasErrors)
    {
    StringBuilder errors = new StringBuilder("Compiler Errors :rn");
    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;
    }
    } </pre>
    <br/>
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    <span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left and i'm getting some exceptions like this:</span>
    <pre class="prettyprint {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?)}</pre>
    <br/>
    <br/>
    <br/>
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    Any help?
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    Thank you.
    <p style="padding-right:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:14px; vertical-align:baseline; clear:both; word-wrap:break-word; font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; text-align:left
    <span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left </span><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left </span><span style="font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif; font-size:14px; text-align:left </span><br/>

    <
    -- Hamid Eghbalz Technical Manager Douran Co
    <br/>
    <br/>
    <br/>

    View the full article
     
    #1

Share This Page