问题简单描述:discriminator 对应的列如何映射,如果不做映射,discriminator对应的列的内容读取不到,如果做了,插入数据不正确。详细如下:
先看Order 和 Invoice的配置文件:
Order是一个聚合根,保存Invoice(发票)信息,理论上都应该通过Order进行操作,那么我们再看Order的仓储:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void Store(Order order)
{
ISession session = Session;
try
{
session.Clear();
session.Save(order);
session.Flush();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
}
}
{
ISession session = Session;
try
{
session.Clear();
session.Save(order);
session.Flush();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
finally
{
}
}
我进行Order的插入操作,同时插入Invoice
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Guid id = Guid.NewGuid();
Order order = new Order() {
ID = id,
Code = DateTime.Now.ToString()
};
order.Invoice = new InvoiceEnterprise() { ID = order.ID, InvoiceType = InvoiceType.NormalEnterprise, EnterpriseName = "test", Content = "明细" };
IOrderRepository orderRepository = RepositoryFactory.Get<IOrderRepository, Order>();
orderRepository.Store(order);
Order order = new Order() {
ID = id,
Code = DateTime.Now.ToString()
};
order.Invoice = new InvoiceEnterprise() { ID = order.ID, InvoiceType = InvoiceType.NormalEnterprise, EnterpriseName = "test", Content = "明细" };
IOrderRepository orderRepository = RepositoryFactory.Get<IOrderRepository, Order>();
orderRepository.Store(order);
测试结果正常,能正常插入数据,
我们再看看读取数据:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
IOrderRepository orderRepository = RepositoryFactory.Get<IOrderRepository, Order>();
Order order = orderRepository.FindById(new Guid("718523d4-f794-4cad-8c5b-d2e76c9eedee"));
Console.WriteLine("InvoiceType="+((int)order.Invoice.InvoiceType).ToString());
Order order = orderRepository.FindById(new Guid("718523d4-f794-4cad-8c5b-d2e76c9eedee"));
Console.WriteLine("InvoiceType="+((int)order.Invoice.InvoiceType).ToString());
测试结果显示:InvoiceType=0 而数据库里,很明显是1,无论数据库里是什么,都未能映射到 InvoiceType
那么我就对 InvoiceType 进行映射吧:
再做一次测试,发现插入数据时:
此 SqlParameterCollection 的 Count=3 的索引 3 无效。
显然是 InvoiceType做了多余的映射,
查询数据结果:
InvoiceType=1
查询结果是正确的。
经过上面的测试,我就迷糊了,不知道怎么配置,才能让插入和查询都能正常呢?