我正在尝试按代码映射和加入实体。我正在使用northwind数据库练习并获得有关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();
}
}
我已经发现我可以通过这个
映射我的所有类图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 :(得分:0)
查看您的代码段 - 这看起来像命名空间问题。您只在命名空间NhiberNiteDemo
中包含Customers类,尝试移动命名空间的结束}
以封装上面的所有代码。
如果这不能解决问题,则需要确保正在加载映射。尝试从Customers
实体中删除Order
属性,然后更改查询,以便在Orders
实体上进行直接选择,看看它是否正常运行。这将确定映射是否已正确加载。