[英]fluent nhibernate mapping many to many configuration error
我在流畅的休眠世界中使用HasManyToMany方法感到困惑。 这是我的“数据库”应用程序。 在这个数据库中,我有一些测试数据。 现在,我想通过Query方法询问数据库,哪个用户可以对应用程序使用什么。
public class Application
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<User> User { get; set; }
public Application()
{
User = new List<User>();
}
}
public class User
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Application> Application { get; set; }
public User()
{
Application = new List<Application>();
}
}
public class ApplicationMap : ClassMap<Application>
{
public ApplicationMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.User)
.Table("Access")
.ParentKeyColumn("user_fk")
.ChildKeyColumn("id")
.Cascade.All();
}
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.Application)
.Table("Access")
.ParentKeyColumn("application_fk")
.ChildKeyColumn("id")
.Cascade.All();
}
}
这是我的数据访问方法:
public static List<Application> SelectApplicationByUser(int userId) { var session = SessionManager.CurrentSession; return session.Query<Application>().Where(x => x.User.Any(y => y.Id == userId)).ToList(); }
现在,我得到了错误的未知列用户ID。 有人可以给我一些帮助吗,我已经搜索了很长时间并且使用了许多方法,但是没有任何效果。 我的映射定义有什么问题? 也许我的Linq陈述也是错误的。 谢谢
在“父级”列中可以搜索所有者ID(应用程序)。 子项是用户表中要搜索的参考ID的位置。
public ApplicationMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.User)
.Table("Access")
.ParentKeyColumn("application_fk")
.ChildKeyColumn("user_fk")
.Cascade.All();
}
与用户相似
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.Application)
.Table("Access")
.ParentKeyColumn("user_fk")
.ChildKeyColumn("application_fk")
.Inverse() // this is essential
.Cascade.All();
}
注意:集合之一必须映射为Inverse()
。 这样可以正确解决插入,更新顺序的问题。
注意2:非常重要的注意事项。 级联最有可能不需要。 在多对多关系中,配对表总是被处理。 在这种情况下,级联意味着在更改应用程序时确实更改了所有引用的用户实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.