2015-12-17 44 views
0

我正在处理一个C#项目,从而在运行时从给定服务器上的给定数据库动态构建ORM解决方案的映射,即nHibernate。目前,我正致力于获取基本对象和关系映射类运行时生成工作。我能够通过CodeDOM的内存编译在运行时下面的类:在运行时从运行时生成的另一个对象生成的调用类

namespace DataDictionary.Domain 
{ 
    public class Entity 
    { 
     public virtual int EntityID { get; set; } 
     public virtual string EntityName { get; set; } 
     public virtual DateTime EntityFoundationDate { get; set; } 
    } 
} 

然而,当我尝试使用的CodeDOM编译下面的类相当于:

using System; 
using System.Linq; 
using NHibernate.Cfg; 
using NHibernate.Cfg.MappingSchema; 
... 
namespace DataDictionary.Domain 
{ 
    public class EntityMap : ClassMapping<Entity> 
    { 
     public EntityMap() 
     { 
      this.Table(Entity); 
      this.ID(p => p.EntityID); 
      this.Property(p => p.EntityName); 
      this.Property(p => p.EntityFoundationDate); 
     } 
    } 
} 

我得到的编译时以下错误:error CS0246: The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?)

我的问题是为什么Entity,尽管被放置在运行时编译正确的命名空间,没有被看到。

此外,仅供参考,这里是我用来创建EntityMap方法:

public static object CreateNewObject(ref object table, AssemblyName domain, params FieldInfo[] columns) 
    { 
     if (columns == null || columns.Length == 0) 
      return null; 

     string tableName = table.GetType().Name; 

     //check to see if a class exists which both matches the name of `table` 
     //and whose full name contains the correct assembly to be used. 
     var namespaceCheck = (from assembly in AppDomain.CurrentDomain.GetAssemblies() 
           from type in assembly.GetTypes() 
           where type.Name == tableName && Utilities.GetFriendlyAssemblyName(type.FullName).Equals("DataDictionary.Domain") 
           select type).FirstOrDefault(); 

     if (namespaceCheck == null) 
      throw new InvalidOperationException("Valid type not found."); 

     StringBuilder builder = new StringBuilder("using NHibernate; using NHibernate.Cfg; using NHibernate.Cfg.MappingSchema; using NHibernate.Dialect; using NHibernate.Mapping.ByCode; using NHibernate.Mapping.ByCode.Conformist; "); 

     builder.Append("namespace DataDictionary.Domain{"); 

     builder.AppendFormat("public class {0}Map : ClassMapping<{0}> {{", tableName); 
     builder.AppendFormat("public {0}Map(){{\n",tableName); 
     builder.AppendFormat("this.Table({0});", tableName); 

     //find the ID column. 
     var list = columns.Where(x => x.Name.ToUpper().Contains("ID")); 
     builder.AppendFormat("this.ID(p => p.{0})", Utilities.cleanFieldName(list.ElementAt(0).Name)); 
     columns = columns.Where(x => !x.Equals(list.ElementAt(0))).ToArray(); 

     //map the properties. 
     foreach (FieldInfo column in columns) 
     { 
      builder.AppendFormat("this.Property(p => p.{0});", Utilities.cleanFieldName(column.Name)); 
     } 

     //close all open brackets. 
     builder.Append("}}}"); 

     //send code to helper class for runtime compilation via CodeDOM. 
     return CodeDOM_Helpers.Execute(builder.ToString(), tableName,domain.Name); 
    } 

回答

0

我想你打算也许是这样的:

public EntityMap() 
    { 
     this.Table(Entity); 
     this.ID(p => p.Entity); 
     this.Property(p => p.EntityName); 
     this.Property(p => p.EntityFoundationDate); 
    } 

为阅读:

public EntityMap() 
    { 
     this.Table(Entity); 
     this.ID(p => p.EntityID); // since the type of p does not have an "Entity" property 
     this.Property(p => p.EntityName); 
     this.Property(p => p.EntityFoundationDate); 
    } 
+0

固定在OP中;感谢您指出了这一点! – Expack3

+0

另外,想补充一点,这并没有解决我的问题,因为我仍然得到相同的错误。 (这是有道理的,因为它是映射类中的一个错字,当映射类实际上看到对象类时它会触发。) – Expack3