简体   繁体   中英

fluent nhibernate mapping many to many configuration error

I struggle with the HasManyToMany method in the fluent nhibernate world. Here is my "database" application. In this database I have some test data. Now I want to ask the database via the Query method which User can use what for a Application. 在此处输入图片说明

here are my model classes:

    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>();
        }
    }

and here is the mapping code:

    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();
        }
    }

here are my data access method:

  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(); } 

Now I get the error unknown column user id. Can someone give me some kick, I have search so long and used many approaches, but nothing has worked. What is wrong with my mapping definiton? Or maybe my Linq statement is also wrong. Thanks

The Parent column is where to search for holder id (application). the child is where is the reference id to be searched in user table.

public ApplicationMap()
{
   Id(x => x.Id);
   Map(x => x.Name);
   HasManyToMany(x => x.User)
       .Table("Access")
       .ParentKeyColumn("application_fk")
       .ChildKeyColumn("user_fk")
       .Cascade.All();
}

And similar for user

    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();
    }

NOTE: One of the collection must be mapped as Inverse() . That will correctly solve issues with the order of insert, update...

NOTE2: Very important note. The Cascade all is most likely not needed. In many to many relations, the pair table is handled always. The cascade in this case means do change all referenced user entities, when changing the application.

Read here more: 23.2. Author/Work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM