Fluent-nhibernate:未获取记录

发布于 2024-10-11 16:59:15 字数 1815 浏览 0 评论 0原文

我有一个类似的实体

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2024-10-18 16:59:15

您的 SKU 地图错误。为什么将 PRODUCT_NAME 定义为 Id 列?您需要通过将 Id 设置为 Id 列(您已将其注释掉)来修复此问题:

Id(x => x.Id, "NAME_OF_YOUR_ID_COLUMN_HERE").GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");

如果 PRODUCT_NAME 确实是 Id,则需要像这样设置它:

Id(x => x.Ptoduct, "PRODUCT_NAME").GeneratedBy.Assigned();

并删除另一行:

Map(x => x.Ptoduct, "PRODUCT_NAME");

另外,如果您的数据库有更多的字段或表,那么您正在映射,它可能会给您带来很多错误。要解决这些问题,您需要在配置中将 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):

Id(x => x.Id, "NAME_OF_YOUR_ID_COLUMN_HERE").GeneratedBy.Assigned();
Map(x => x.Ptoduct, "PRODUCT_NAME");

If PRODUCT_NAME is indeed the Id, you need to set it like this:

Id(x => x.Ptoduct, "PRODUCT_NAME").GeneratedBy.Assigned();

and remove the other line:

Map(x => x.Ptoduct, "PRODUCT_NAME");

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 to false 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 same Ptoduct will return the topmost object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文