如何使用 Nhibernate ManyToMany 添加记录?

How do I add records using Nhibernate ManyToMany?

我有演员和电影class。一个演员可以演很多部电影,一部电影可以有很多演员。所以我写了这些 class 和 mappings

class Actor
{
    public Actor()
    {
        Movies = new List<Movie>();
    }
    public virtual int ActorId { get; set; }
    public virtual string ActorName { get; set; }
    public virtual IList<Movie> Movies { get; set; }
}


class ActorMap : ClassMap<Actor>
{
    public ActorMap ()
    {
        Id(x => x.ActorId).GeneratedBy.Identity();
        Map(x => x.Name);
        HasManyToMany(x => x.Movies)
            .Cascade.All()
            .Table("ActorMovies");
    }
}


class Movie
{
    public virtual Guid MovieId { get; set; }
    public virtual string MovieName { get; set; }
    public virtual IList<Actor> Actors { get; set; }
}


class MovieMap : ClassMap<Movie>
{
    public MovieMap ()
    {
        Id(x => x.MovieId).GeneratedBy.GuidComb();
        Map(x => x.Name);
        HasManyToMany(x => x.Actors)
            .Cascade.All()
            .Inverse()
            .Table("ActorMovies");
    }
}

和数据库连接

public class Demo
{
    private static ISessionFactory _sessionFactor;
    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactor == null)
            {
                InitializeSessionFactory();
            }
            return _sessionFactor;
        }
    }

    private static void InitializeSessionFactory()
    {
        _sessionFactor = Fluently.Configure()
        .Database(MySQLConfiguration.Standard
        .ConnectionString(
            "Server=localhost;Database=movie;Uid=xx;Pwd=xx;"
        ))
        .Mappings(mappings => mappings.FluentMappings
        .AddFromAssemblyOf<Program>())
        .BuildSessionFactory();
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

我卡在这一点上了。如何添加演员和电影?在考虑创建演员时,应该已经有 he/she 播放的电影,在这种情况下,如果我先添加电影,同样的问题仍然会出现,因为我需要演员。我是数据库操作和 nhibernate 的新手,如果答案很明显,我很抱歉。

class Program
{
    static void Main(string[] args)
    {
        using var session = Demo.OpenSession();
        using var transaction = session.BeginTransaction();

        Actor actor = new Actor();

        transaction.Commit();
    }
}

您可以尝试这样的操作:

  1. 创建影片对象

         Movie killBill = new Movie {
            Name = "Kill Bill"
        };
    
  2. 创建 Actor 对象

        Actor umaThurman = new Actor
        {
            Name = "Uma Thurman",
        };
    
  3. 在你的 actor 对象中将已创建的电影对象添加到 Movies 中:

        umaThurman.Movies.Add(killBill);
    
  4. 在您的电影对象中将已创建的演员对象添加到 Actors:

        killBill.Actors.Add(umaThurman);
    
  5. 还有下一位演员:

        Actor davidCarradine = new Actor
        {
            Name = "David Carradine",
        };
    
        davidCarradine.Movies.Add(killBill);
        killBill.Actors.Add(davidCarradine);
    
  6. 只需使用会话的保存方法传递 killBill 电影对象(保存一次,NHibernate 将完成所有工作,它将保存 killBill 电影对象和您以两种方式关联的演员:电影与演员和演员与电影):

        session.Save(killBill);
    
  7. 然后提交: transaction.Commit();

PS.: 不要忘记在 Actor 和 Movie 中初始化你的列表 类:

// Actor Class
public virtual IList<Movie> Movies { get; set; } = new List<Movie>();

// Movie Class
public virtual IList<Actor> Actors { get; set; } = new List<Actor>();