0

我有一个问题,我的表结构中有一个多对多映射,在尝试编辑简单记录时会让人头疼。

我遇到问题的示例布局:

Facilities Many-to-One Locations
Facilities One-to-Many Users
Users Many-to-Many Locations
Users One-to-Many PreviousPasswords

如果我对设施记录进行更改(更改名称字段),我会在保存时收到以下错误:

集合 [Users.PreviousPasswords] 未由 flush() 处理

映射看起来像:

public FacilitiesMap()
{
    Table("Facilities");

    Id(x => x.ID);
    Map(x => x.Name);
    HasMany(x => x.Users).KeyColumn("FacilitiesID").Cascade.AllDeleteOrphan().Inverse();
    HasMany(x => x.Locations).KeyColumn("FacilitiesID").Cascade.AllDeleteOrphan().Inverse();
}

public UsersMap()
{
    Table("Users");
    Id(x => x.ID);
    Map(x => x.FirstName);
    Map(x => x.LastName);
    References(x => x.Facilities, "FacilitiesID").ForeignKey("ID");
    HasMany(x => x.PreviousPasswords).KeyColumn("UsersID").Cascade.AllDeleteOrphan().Inverse();
    HasManyToMany<Locations>(x => x.Locations)
         .Schema("Members")
         .Table("UsersToLocations")
         .ParentKeyColumn("UsersID")
         .ChildKeyColumn("LocationsID")
         .LazyLoad()
         .Cascade.SaveUpdate().Inverse();
}

public LocationsMap()
{
    Table("Locations");

    Id(x => x.ID);
    Map(x => x.Name);
    References(x => x.Facilities, "FacilitiesID").ForeignKey("ID");
    HasMany(x => x.Patients).KeyColumn("LocationsID").Cascade.AllDeleteOrphan().Inverse();
    HasManyToMany<Users>(x => x.Users)
         .Schema("Members")
         .Table("UsersToLocations")
         .ParentKeyColumn("LocationsID")
         .ChildKeyColumn("UsersID")
         .Cascade.SaveUpdate();
}

public PreviousPasswordsMap()
{
    Table("PreviousPasswords");

    Id(x => x.ID);
    Map(x => x.Password);
    Map(x => x.DateTime);

    References(x => x.Users, "UsersID").ForeignKey("ID");
}

成功更新设施记录的唯一方法是在更改和保存记录之前使用以下函数获取记录:

public Facilities GetFacility(int id)
{
    return FluentSessionManager.GetSession()
        .CreateCriteria<Facilities>()
        .Add(Expression.Eq("ID", id))
        .SetFetchMode("Users", FetchMode.Eager)
        .SetFetchMode("Locations", FetchMode.Eager)
        .UniqueResult<Facilities>();
}

此方法的问题在于,如果有 10,000 个用户,则处理此查询需要很长时间。或者更糟糕的是,如果我们也有 100 个位置,则需要大约 2 分钟来编辑一条设施记录。

我确信映射中存在某种问题。不知道如何修复,甚至不知道从哪里开始。对此的任何帮助将不胜感激。提前致谢。

4

1 回答 1

0

您真的需要该设施的所有用户吗?当您只添加用户时,您可以使用

HasMany(x => x.Users).ExtraLazyLoad();

并在确实需要所有子集合时改进查询

public Facilities GetFacility(int id)
{
    var session = FluentSessionManager.GetSession();

    // ignore the result, we only want to cache the results in the session
    session.CreateCriteria<Facilities>()
        .Add(Expression.Eq("ID", id))
        .SetFetchMode("Users", FetchMode.Eager)
        .Future<Facilities>();

    return session.CreateCriteria<Facilities>()
        .Add(Expression.Eq("ID", id))
        .SetFetchMode("Locations", FetchMode.Eager)
        .FutureValue<Facilities>().Value;
}
于 2013-01-08T08:12:49.737 回答