我正在与流利的 nhibernate 作斗争Fluent Nhibernate 的流利的 nhibernate 多对多映射和额外的列
我已经复制了映射并编写了我能写的最小程序……但它不会保存……有人能提供一些见解吗???
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Inventory> Inventory { get; set; }
public Product()
{
Inventory = new List<Inventory>();
}
}
public class Warehouse
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Inventory> Inventory { get; set; }
public Warehouse()
{
Inventory = new List<Inventory>();
}
}
public class Inventory
{
public Product Product { get; set; }
public Warehouse Warehouse { get; set; }
public bool StockInHand { get; set; }
// override object.Equals
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
var i = obj as Inventory;
return ((i.Product.Id == this.Product.Id)
&& (i.Warehouse.Id == this.Warehouse.Id));
}
// override object.GetHashCode
public override int GetHashCode()
{
return 9999;
}
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Not.LazyLoad();
Table("Product");
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Name);
HasMany(x => x.Inventory).AsBag()
.Cascade.All()
//.Inverse()
.Table("Inventory");
}
}
public class WarehouseMap : ClassMap<Warehouse>
{
public WarehouseMap()
{
Not.LazyLoad();
Table("Warehouse");
Id(x => x.Id).GeneratedBy.Assigned();
Map(x => x.Name);
HasMany(x => x.Inventory).AsBag()
.Cascade.All()
.Inverse()
.Table("Inventory");
}
}
public class InventoryMap : ClassMap<Inventory>
{
public InventoryMap()
{
Not.LazyLoad();
Table("Inventory");
CompositeId()
.KeyReference(x => x.Product, "Product_id")
.KeyReference(x => x.Warehouse, "Warehouse_id");
Map(x => x.StockInHand);
}
}
而节目...
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
Product p = new Product() { Id = 1, Name="product" };
Inventory i = new Inventory() { StockInHand = true };
i.Product = p;
p.Inventory.Add(i);
Warehouse w = new Warehouse() { Id = 1, Name = "warehouse" };
i.Warehouse = w;
w.Inventory.Add(i);
session.SaveOrUpdate(p);
session.Flush();
transaction.Commit();
}
}
我得到的例外是
constraint failed\r\nforeign key constraint failed
我还输出了 create 语句,这对我来说看起来是正确的......
create table Inventory (
Product_id INT not null,
Warehouse_id INT not null,
StockInHand BOOL,
primary key (Product_id, Warehouse_id),
constraint FK2B4C61665C5B845 foreign key (Product_id) references Product,
constraint FK2B4C616A6DE7382 foreign key (Warehouse_id) references Warehouse)
create table Product (
Id INT not null,
Name TEXT,
primary key (Id)
)
create table Warehouse (
Id INT not null,
Name TEXT,
primary key (Id)
)
以及在异常之前运行的 SQL....
NHibernate:
INSERT
INTO
Warehouse
(Name, Id)
VALUES
(@p0, @p1);
@p0 = 'warehouse' [Type: String (0)], @p1 = 1 [Type: Int32 (0)]
NHibernate:
INSERT
INTO
Inventory
(StockInHand, Product_id, Warehouse_id)
VALUES
(@p0, @p1, @p2);
@p0 = True [Type: Boolean (0)], @p1 = 1 [Type: Int32 (0)], @p2 = 1 [Type: Int32 (0)]
那么这应该如何正常工作?!?