流畅的nhibernate映射有很多使用propertyref而不使用fetch.join

发布于 2024-12-01 02:33:18 字数 1928 浏览 1 评论 0原文

我正在使用 Nhibernate 3.2 以及与 NH 3.2 兼容的 FluentNhibernate 版本,并且我已经来映射我的系统的遗留部分。我相信可以做到我所要求的,但需要一些帮助来正确映射它。

我在下面有一个用户类,其中包含应用程序列表。

public class User 
{
    public virtual string UserID { get; set; }
    public virtual int Id { get; set; }
    public virtual IList<Application> Applications { get; set; }
}

我还有一个应用程序类,它有一个返回用户类的外键,它不是“主键”。看下面

public class Application 
{
    // Not sure if this 'User' is needed?
    public virtual User User { get; set; }
    public virtual int IdFromUserTable { get; set; }
    public virtual string ApplicationName { get; set; }

}

我有相应的ClassMap,我认为UserMap是问题所在

UserMap

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserID);
        HasMany(x => x.Applications)
            .AsBag()
            .KeyColumn("UserID")
            .PropertyRef("Id")
            .LazyLoad()
            .Inverse();
        Table("usertable");
        ReadOnly();
    }
}

ApplicationMap

public class ApplicationMap : ClassMap<Application>
{
    public ApplicationMap()
    {
        CompositeId()
            .KeyProperty(x => x.ApplicationName)
            .KeyProperty(x => x.IdFromUserTable, "UserID");   
        Table("applicationstable");
    }
}

Table结构如下:

User Table

Primary Key - string, ColumnName = UserID
int (Identity) - int, ColumnName = Id

< strong>应用表

Composite Key - string, ColumnName = ApplicationName
and
int, ColumnName = UserId (references Id column in user table)

我的问题是如何使映射适用于上述场景。

一个警告是:我无法更改数据库的结构,但我可以更改类/类映射

任何帮助都非常感谢..

PS - 我确实通过在 HasMany userMap 中添加 Fetch.Join 来使其工作,但我更喜欢在需要时延迟评估应用程序列表

谢谢,马克

I am using Nhibernate 3.2, along with a build of FluentNhibernate compatible with NH 3.2 and I have come to map a legacy part of my system. I believe it is possible to do what I require, but need some assistance in mapping it correctly.

I have a User Class below, with a list of Applications.

public class User 
{
    public virtual string UserID { get; set; }
    public virtual int Id { get; set; }
    public virtual IList<Application> Applications { get; set; }
}

I also have a Application class which has a foreign key back to the User Class which is not the "primary key". See Below

public class Application 
{
    // Not sure if this 'User' is needed?
    public virtual User User { get; set; }
    public virtual int IdFromUserTable { get; set; }
    public virtual string ApplicationName { get; set; }

}

I have the corresponding ClassMaps, I think the UserMap is where the issue lies

UserMap

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.UserID);
        HasMany(x => x.Applications)
            .AsBag()
            .KeyColumn("UserID")
            .PropertyRef("Id")
            .LazyLoad()
            .Inverse();
        Table("usertable");
        ReadOnly();
    }
}

ApplicationMap

public class ApplicationMap : ClassMap<Application>
{
    public ApplicationMap()
    {
        CompositeId()
            .KeyProperty(x => x.ApplicationName)
            .KeyProperty(x => x.IdFromUserTable, "UserID");   
        Table("applicationstable");
    }
}

The Table structures are as follows:

User Table

Primary Key - string, ColumnName = UserID
int (Identity) - int, ColumnName = Id

Applications Table

Composite Key - string, ColumnName = ApplicationName
and
int, ColumnName = UserId (references Id column in user table)

My question is how to get the mapping to work for the above scenario.

One caveat is: I cannot change the structure of the database, but I can change the classes / classmaps

Any help greatly appreciated..

PS - I did get this to work by adding in Fetch.Join in the HasMany userMap, but I would prefer to lazily evaluate the Application List when needed

Thanks, Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文