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]]
任何人都可以幫助確定這個錯誤的原因?