Fluent-nhibernate:未获取记录
我有一个类似的实体
public class SKU
{
//public int Id { get; set; }
public string FactoruCode { get; set; }
public string Ptoduct { get; set; }
}
,映射定义为
public class SKUMap : ClassMap<SKU>
{
public SKUMap()
{
Table("MST_PRODUCT");
Not.LazyLoad();
Id(x => x.Ptoduct).GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");
Map(x => x.FactoruCode, "FACTORY_CODE");
}
}
并检索类似的记录,
class Program
{
static void Main()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
var skus = session.CreateCriteria(typeof(SKU)).List<SKU>();
foreach (var sku in skus)
{
Console.WriteLine(sku.Ptoduct);
}
}
}
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle10
.ConnectionString(c =>
c.Is(
@"DATA SOURCE=SERVER_NAME;PERSIST SECURITYINFO=True;USER ID=USER_ID;Password=PWD"));
return Fluently.Configure()
.Database(cfg).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema).BuildSessionFactory();
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config).Create(false, true);
}
}
但表的列数多于为实体指定的列数。此代码执行良好,但我无法获取 SKU 列表(表有超过 8000 行)。
请帮助我理解这个问题。
I have an entity like
public class SKU
{
//public int Id { get; set; }
public string FactoruCode { get; set; }
public string Ptoduct { get; set; }
}
and mapping defined as
public class SKUMap : ClassMap<SKU>
{
public SKUMap()
{
Table("MST_PRODUCT");
Not.LazyLoad();
Id(x => x.Ptoduct).GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");
Map(x => x.FactoruCode, "FACTORY_CODE");
}
}
and retrieving the records like
class Program
{
static void Main()
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (session.BeginTransaction())
{
var skus = session.CreateCriteria(typeof(SKU)).List<SKU>();
foreach (var sku in skus)
{
Console.WriteLine(sku.Ptoduct);
}
}
}
}
private static ISessionFactory CreateSessionFactory()
{
var cfg = OracleClientConfiguration.Oracle10
.ConnectionString(c =>
c.Is(
@"DATA SOURCE=SERVER_NAME;PERSIST SECURITYINFO=True;USER ID=USER_ID;Password=PWD"));
return Fluently.Configure()
.Database(cfg).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema).BuildSessionFactory();
}
private static void BuildSchema(NHibernate.Cfg.Configuration config)
{
new SchemaExport(config).Create(false, true);
}
}
but the table has more columns than specified for entity. This code executes well, but I'm not able to get the list of SKUs (table has more than 8000 rows).
Please help me to understand the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 SKU 地图错误。为什么将
PRODUCT_NAME
定义为 Id 列?您需要通过将 Id 设置为 Id 列(您已将其注释掉)来修复此问题:如果
PRODUCT_NAME
确实是 Id,则需要像这样设置它:并删除另一行:
另外,如果您的数据库有更多的字段或表,那么您正在映射,它可能会给您带来很多错误。要解决这些问题,您需要在配置中将
use_proxy_validator
设置为false
。编辑:
NHibernate 需要 Id 列才能正常工作。我什至不知道它是否在没有实际声明为 Id 列的列的情况下起作用。即使您将
Ptoduct
声明为 Id 列,您也将无法正确查询数据库,因为查询具有相同Ptoduct
的所有对象中的任何一个都会返回最顶层的对象。Your SKU map is wrong. Why have you defined
PRODUCT_NAME
as an Id column? You need to fix it by setting the Id to an Id column (which you have commented out):If
PRODUCT_NAME
is indeed the Id, you need to set it like this:and remove the other line:
Also, if your database has more fields or tables then you are mapping, it can give you many errors. To resolve them, you need to set
use_proxy_validator
tofalse
in your configuration.EDIT:
NHibernate requires an Id column to work properly. I don't even know that if it does work without actually having a column declared as an Id column. Even if you declare
Ptoduct
as an Id column, you will not be able to properly query the database as querying for any of all objects with the samePtoduct
will return the topmost object.