2015-11-11 130 views
1

我試圖通過代碼和連接實體進行映射。我正在使用northwind數據庫來練習和獲取有關nHibernate的知識。我一直在處理這個問題幾個小時,並決定伸出援助之手。流利的nHibernate映射 - 錯誤與未映射的類

我試圖從Northwind映射兩個表,訂單和客戶,並將它們與QueryOver一起加入。這是一個簡單的控制檯應用程序,我的代碼看起來像這樣。

using System; 
using NHibernate.Mapping.ByCode.Conformist; 
using NHibernate.Mapping; 
using NHibernate.Linq; 
using NhiberNiteDemo; 


namespace NhiberNiteDemo 
{ 
    public class Customers { 
    public Customers() { } 
    public virtual string Customerid { get; set; } 
    public virtual string Companyname { get; set; } 
    public virtual string Contactname { get; set; } 
    public virtual string Contacttitle { get; set; } 
    public virtual string Address { get; set; } 
    public virtual string City { get; set; } 
    public virtual string Region { get; set; } 
    public virtual string Postalcode { get; set; } 
    public virtual string Country { get; set; } 
    public virtual string Phone { get; set; } 
    public virtual string Fax { get; set; } 
    public virtual ISet<Order> Orders { get; set; } 
} 


} 
public class CustomersMap : ClassMapping<Customers> { 

    public CustomersMap() { 
     Schema("dbo"); 
     Lazy(true); 
     Id(x => x.Customerid, map => map.Generator(Generators.Assigned)); 
     Property(x => x.Companyname, map => map.NotNullable(true)); 
     Property(x => x.Contactname); 
     Property(x => x.Contacttitle); 
     Property(x => x.Address); 
     Property(x => x.City); 
     Property(x => x.Region); 
     Property(x => x.Postalcode); 
     Property(x => x.Country); 
     Property(x => x.Phone); 
     Property(x => x.Fax); 
    // Bag(x => x.Customercustomerdemo, colmap => { colmap.Key(x =>  x.Column("CustomerID")); colmap.Inverse(true); }, map => { map.OneToMany(); }); 
     Set(x => x.Orders, colmap => { colmap.Key(x => x.Column("CustomerID")); colmap.Inverse(true); }, map => { map.OneToMany(); }); 
    } 
} 
public class Order 
{ 
    public Order() { } 
    public virtual int Orderid { get; set; } 
    public virtual ISet<Customers> customers { get; set; } 
    // public virtual Employees Employees { get; set; } 
    //public virtual Shippers Shippers { get; set; } 
public virtual DateTime Orderdate { get; set; } 
public virtual DateTime Requireddate { get; set; } 
public virtual DateTime Shippeddate { get; set; } 
public virtual decimal Freight { get; set; } 
public virtual string Shipname { get; set; } 
public virtual string Shipaddress { get; set; } 
public virtual string Shipcity { get; set; } 
public virtual string Shipregion { get; set; } 
public virtual string Shippostalcode { get; set; } 
public virtual string Shipcountry { get; set; } 
} 




public class OrdersMap : ClassMapping<Order> 
{ 

public OrdersMap() 
{ 
    Schema("dbo"); 
    Lazy(true); 
    Id(x => x.Orderid, map => map.Generator(Generators.Identity)); 
    Property(x => x.Orderdate); 
    Property(x => x.Requireddate); 
    Property(x => x.Shippeddate); 
    Property(x => x.Freight); 
    Property(x => x.Shipname); 
    Property(x => x.Shipaddress); 
    Property(x => x.Shipcity); 
    Property(x => x.Shipregion); 
    Property(x => x.Shippostalcode); 
    Property(x => x.Shipcountry); 
    ManyToOne(x => x.customers, map => 
    { 
     map.Column("CustomerID"); 
     map.NotNullable(true); 
     map.Cascade(Cascade.None); 
    }); 
/* 
    ManyToOne(x => x.Employees, map => 
    { 
     map.Column("EmployeeID"); 
     map.PropertyRef("Employeeid"); 
     map.NotNullable(true); 
     map.Cascade(Cascade.None); 
    }); 
    */ 
    /* 
    ManyToOne(x => x.Shippers, map => 
    { 
     map.Column("ShipVia"); 
     map.NotNullable(true); 
     map.Cascade(Cascade.None); 
    }); 
     */ 
    // Set(x => x.OrderDetails, colmap => { colmap.Key(x => x.Column("OrderID")); colmap.Inverse(true); }, map => { map.OneToMany(); }); 
} 
} 


public class Program 
{ 
    private static Lazy<ISessionFactory> factory = new Lazy<ISessionFactory>(GetSessionFactory, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication); 

    public static void Main(string[] args) 
    { 
     // using (ISession session = OpenSession()) 
     { 
     // IList<Customers> Customers = session.Query<Customers>().Where(p => p.Customerid == "ALFKI").ToList(); 
      //IList<Pet> pets = query.List<Pet>(); 
     // // Console.Out.WriteLine("pets.Count = " + pets.Count); 
     //  Customers.ToList().ForEach(p => Console.WriteLine(p.Contactname)); 
     //  Console.Read(); 
     } 
     Testhib(); 

    } 

    public static void Testhib() 
    { 
     ISession session = OpenSession(); 
     session.QueryOver<Order>() 
      .JoinQueryOver<Customers>(p => p.customers).Where(k => k.Customerid == "ALFKI"); 


     //Console.WriteLine("Jow Jow"); 
     // Console.Read(); 

    } 


    private static ISession OpenSession() 
    { 
     return factory.Value.GetCurrentSession(); 
    } 

    private static ISessionFactory GetSessionFactory() 
    { 
     //NHibernate.Cfg.Configuration 
     var c = new Configuration(); 
     //c.Configure(); 
     c.DataBaseIntegration(db => 
     { 
      db.ConnectionString = "Server=\"nsure-shj\";database=NORTHWND;Integrated Security=SSPI"; 
      db.Dialect<NHibernate.Dialect.MsSql2012Dialect>(); 

     }); 
     //c.Configure("c:\XML.xml"); 

     ModelMapper maps = new ModelMapper(); 
     // maps.AddMapping<CustomersMap>(); 
     maps.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes()); 

     c.AddMapping(maps.CompileMappingForAllExplicitlyAddedEntities()); 
     c.CurrentSessionContext<NHibernate.Context.ThreadLocalSessionContext>(); 
     //c.Configure().Configure(); 
     var sessionFac = c.BuildSessionFactory(); 
     return sessionFac; 

     //return sessionFac.GetCurrentSession(); 

    } 



} 

我已經想通了,我可以用我所有的classmaps地圖這

maps.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes()); 

但是,當,當我運行這段代碼,它拋出從BuildSessionFactory方法異常,並以下消息:

An association from the table Order refers to an unmapped class: System.Collections.Generic.ISet`1[[NhiberNiteDemo.Customers, NhiberNiteDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 

任何人都可以幫助確定這個錯誤的原因?

回答

0

看着你的代碼片段 - 這看起來像一個命名空間問題。您只在名稱空間NhiberNiteDemo中附上了Customers類,請嘗試移動該名稱空間的閉合}以封裝上面的所有代碼。

如果這樣不能解決問題,則需要確保映射已加載。嘗試從您的Order實體中刪除Customers屬性,然後更改查詢,以便在Orders實體上直接選擇並查看它是否正常工作。這將確定映射是否已正確加載。