我收到以下代码的PropertyAccessException:
public class Category
{
public int? categoryId { get; set; }
public string categoryName { get; set; }
}
public class Stock
{
public int? stockId { get; set; }
public string stockCode { get; set; }
public string stockName { get; set; }
public IList<Category> PublicCategory { get; set; }
public IList<Category> PrivateCategory { get; set; }
public IList<StockCategory> PublicCategoryList
{
get
{
IList<StockCategory> list = new List<StockCategory>();
if (PublicCategory != null)
{
foreach (Category c in PublicCategory)
{
StockCategory sc = new StockCategory() { Category = c, Stock = this, Type = 0 };
list.Add(sc);
}
}
return list;
}
set
{
PublicCategory = new List<Category>();
foreach (StockCategory sc in value)
{
PublicCategory.Add(sc.Category);
}
}
}
public IList<StockCategory> PrivateCategoryList
{
get
{
IList<StockCategory> list = new List<StockCategory>();
if (PrivateCategory != null)
{
foreach (Category c in PrivateCategory)
{
StockCategory sc = new StockCategory() { Category = c, Stock = this, Type = 1 };
list.Add(sc);
}
}
return list;
}
set
{
PrivateCategory = new List<Category>();
foreach (StockCategory sc in value)
{
PrivateCategory.Add(sc.Category);
}
}
}
}
public class StockCategory
{
public Stock Stock {get; set;}
public Category Category { get; set; }
public int Type { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as StockCategory;
if (t == null)
return false;
if (Stock.stockId == t.Stock.stockId && Category.categoryId == t.Category.categoryId)
return true;
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
以下是映射:
<bag name="PublicCategoryList" table="Stock_Category" cascade="all" lazy="false" where="TYPE=0">
<key column="STOCK_ID"/>
<one-to-many class="Category"/>
</bag>
<bag name="PrivateCategoryList" table="Stock_Category" cascade="all" lazy="false" where="TYPE=1">
<key column="STOCK_ID"/>
<one-to-many class="Category" />
</bag>
我无法直接将其映射为多对多,因为在插入映射表时我必须设置一个额外的字段。上述解决方案是解决无法设置其他列的多对多限制。但是,当我执行session.Save(stockObject)时,上面的解决方案给出了一个例外。如下:
NHibernateDAO<Stock, int> par = CreateDAO<Stock, int>();
Stock p = new Stock();
p.stockName = "stock";
p.stockCode = "stockcode";
p.PublicCategory = new List<Category>();
p.PrivateCategory = new List<Category>();
Category cd1 = new Category() { categoryName = "category1" };
Category cd2 = new Category() { categoryName = "category2" };
Category cd3 = new Category() { categoryName = "category3" };
p.PublicCategory.Add(cd1);
p.PublicCategory.Add(cd2);
p.PublicCategory.Add(cd3);
Category cd4 = new Category() { categoryName = "category4" };
Category cd5 = new Category() { categoryName = "category5" };
Category cd6 = new Category() { categoryName = "category6" };
p.PrivateCategory.Add(cd4);
p.PrivateCategory.Add(cd5);
p.PrivateCategory.Add(cd6);
par.Add(p);
Stock p1 = par.GetById(1);
例外情况如下:
“NHibernate.PropertyAccessException:Exception发生了TestSolution.Domain.Category.categoryId的getter ---&gt; System.Reflection.TargetException:Object与目标类型不匹配。\ r \ n在System.Reflection.RuntimeMethodInfo.CheckConsistency(对象目标)\ r \ n在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo culture,Boolean skipVisibilityChecks)\ r \ n在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化)\ r \ n在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo culture)\ r \ n at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object [] index)\ r \ n,位于d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate中的NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target) \ Properties \ BasicPropertyAccessor.cs:207行\ r \ n --- E.内部异常的内容 在HibernateTest.Program.Main的C:\ Arnab \ otherProjects \ POC Mapping \ HibernateTest \ NHibernateDAO.cs:第43行\ r \ n中的HibernateTest.NHibernateDAO`2.Add(TItem项目)中追踪--- \ r \ n (String [] args)在C:\ Arnab \ otherProjects \ POC Mapping \ HibernateTest \ Program.cs:第46行“