我与功能NHibernate奋力从Fluent Nhibernate Many-to-Many mapping with extra column流利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 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 pri或去例外....
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)]
那么这是如何正确工作?!?
有你'WarehouseMap'类的错误。不应该把表(“库存”)变成像表(“仓库”)那样的东西? – mickfold 2013-04-24 21:13:31
@Penfold我根据你的评论更新了这个问题我认为create table语句显示了我在这种情况下应该期待的(我认为) – 0909EM 2013-04-24 21:21:58