Nhibernate attempts to added duplicate column on Save
I am trying to use Fluent NHibernate. I have setup two table Products and Categories. Products has a CategoryID field and a Foreign key that ties CategoryID of Product开发者_如何学运维s to the PK (CategoryID) of the Categories table.
DTO: Product Class:
public class Product
{
[HiddenInput(DisplayValue = false)]
public virtual int ProductId { get; set; }
public virtual string Name { get; set; }
public virtual int CategoryId { get; set; }
[DataType(DataType.MultilineText)]
public virtual string Description { get; set; }
public virtual decimal Price { get; set; }
public virtual decimal SalePrice { get; set; }
public virtual int StockAmt { get; set; }
public virtual bool StockLevelWarning { get; set; }
public virtual string Dimensions { get; set; }
public virtual bool PriceIncludesTax { get; set; }
public virtual string TaxClass { get; set; }
public virtual decimal ProductWeight { get; set; }
public virtual decimal CubicWeight { get; set; }
public virtual string PackageDimensions { get; set; }
public virtual bool IncludeLatestProduct { get; set; }
public virtual Category Category { get; set; }
public Product()
{
Name = String.Empty;
Description = String.Empty;
Price = 0m;
SalePrice = 0m;
StockAmt = 0;
StockLevelWarning = true;
Dimensions = String.Empty;
PriceIncludesTax = false;
TaxClass = String.Empty;
ProductWeight = 0;
CubicWeight = 0;
PackageDimensions = String.Empty;
IncludeLatestProduct = false;
}
}
In my ProductMap class, I have a everything specified according to the Fluent Documentation including the the last property set as a Reference:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Table("Products");
Id(x => x.ProductId);
Map(x => x.Name);
Map(x => x.CategoryId);
Map(x => x.Description);
Map(x => x.Price);
Map(x => x.SalePrice);
Map(x => x.StockAmt);
Map(x => x.StockLevelWarning);
Map(x => x.Dimensions);
Map(x => x.PriceIncludesTax);
Map(x => x.TaxClass);
Map(x => x.ProductWeight);
Map(x => x.CubicWeight);
Map(x => x.PackageDimensions);
Map(x => x.IncludeLatestProduct);
References(x => x.Category);
}
}
DTO: Category:
public class Category
{
public virtual int CategoryId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<Product> Products { get; set; }
public Category()
{
Name = string.Empty;
Description = string.Empty;
}
}
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
Id(x => x.CategoryId);
Map(x => x.Name);
Map(x => x.Description).Column("CategoryDescription");
HasMany(x => x.Products);
}
}
However, when I attempt to do a save on the Product entity, I get a MySQL error in return that I am trying to add the CategoryID twice. When looking at the stack trace, it specifies what columns NHibernate is attempting to save. And indeed, it lists not only CategoryID in the order that I have it specified in the ProductMap class, but also as the last column in the insert statement again.
Error:
"could not insert: [DTOS.Product][SQL: INSERT INTO Products (Name, CategoryId, Description, Price, SalePrice, StockAmt, StockLevelWarning, Dimensions, PriceIncludesTax, TaxClass, ProductWeight, CubicWeight, PackageDimensions, IncludeLatestProduct, Categoryid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"}
I am following the Author to Book example Fluent's documentation uses.
It is almost as if it is equating the property (type Category) on the Product class to the Primary Key of the Category table. But as I mentioned, I am using the same example from the Fluent's example code.
Above answer is correct.
If you need categoryId then do Product.Category.Id.
I would try specifying the column names in your mapping:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Table("Products");
Id(x => x.ProductId);
References(x => x.Category).Column("CategoryId");
}
}
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Table("Categories");
Id(x => x.CategoryId);
HasMany(x => x.Products).KeyColumn("ProductId");
}
}
I noticed the following in the NHibernate generated insert above:
CategoryId and Categoryid are not the same case. Notice the Id
and id
at the end.
well if you look at the mapping closely there are two entries that will create the column categoryid in the db for you.
Map(x => x.CategoryId);
References(x => x.Category);
Well you could remove one of them or if u need them both then set the column name of one of those explcitly something like category_id. if its a general thing across all your references then use a convention that you can specify for your foreign key relation.
also if you look at the sql generated you can see CategoryId
and Cateogryid
the second one being the reference one.
精彩评论