C# 在运行时编译第一个EF4类代码时遇到问题:)
我试图在运行时编译这段代码。此代码是代码第一个EF4类C# 在运行时编译第一个EF4类代码时遇到问题:),c#,c#-4.0,compilation,runtime,C#,C# 4.0,Compilation,Runtime,我试图在运行时编译这段代码。此代码是代码第一个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 {
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; }
}
}
我得到了一些例外,比如:
error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)}
有什么帮助吗
谢谢。您似乎没有在“compilerparams”中设置引用程序集。 很可能需要添加必要的程序集引用,就像在普通VS C#项目中添加一样。尝试使用以下方法在BuildAssembly方法中添加它们:
compilerparams.ReferencedAssembly.Add(“mscorlib.dll”);
compilerparams.ReferencedAssemblys.Add(“System.dll”);
compilerparams.ReferencedAssemblys.Add(“System.Core.dll”);
compilerparams.ReferencedAssemblys.Add(“System.Xml.dll”);
compilerparams.ReferencedAssembly.Add(“EntityFramework.dll”);
除此之外,确保要编译的代码中使用了以下命名空间:
using System.Data.Entity;使用System.Collections.Generic;使用System.ComponentModel.DataAnnotations代码>这应该可以解决您在编译时遇到的问题。我也遇到了这个问题,并将网站从NET 4.0升级到NET 4.6.1,这是固定的。记住删除NUGET组件并在新的NET target新版本中重新安装。我添加了这些代码compilerparams.ReferencedAssemblys.Add(“mscorlib.dll”)。添加(“System.dll”)。添加(“System.Core.dll”)。添加(“System.Xml.dll”)。添加(“System.ComponentModel.DataAnnotations.dll”);并且得到了这些错误。找不到类型或命名空间名称“Column”“”找不到“ColumnAttribute”“”找不到“DatabaseGeneratedAttribute”“”找不到“DatabaseGeneratedAttribute”“”找不到“Table”找不到“TableAttribute”,因为编译器似乎不知道某些EF类。您需要向编译器参数添加EF引用,如:compilerparams.ReferencedAssemblys.add(“EntityFramework.dll”),并确保要编译的代码中使用了以下命名空间:“使用System.Data.Entity;使用System.ComponentModel.DataAnnotations;“我更新了我的答案,也包括了这一点。
error CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)}