Answered by:
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
-
Hi guy,
Please read this blog article talking about hwo to process Code-First Development with Entity Framework 4.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jason Dot Wang Tuesday, May 8, 2012 3:23 AM
Tuesday, May 1, 2012 6:03 AM
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 toSystem.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 towhere the exe is located so that it can be found. That's what happenswhen you reference a .NET DLL. The DLL is copied to the Bin directorywhere the exe is located so that .NET can find it for the application touse, 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 befound by a .NET application. The last I remember, a .NET DLL has to bein the location of the executable in order for it to be found. This mayonly 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 needsto 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.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Jason Dot Wang Tuesday, May 8, 2012 3:23 AM
Tuesday, May 1, 2012 6:03 AM