Reputation: 1157
I have an Actor and Movie class. An actor can play in many movies and a movie can have many actors. So I have written these class and 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");
}
}
And db connections
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();
}
}
I am stuck at this point. How do I add actors and movies? When think of creating an actor, there should already movies in which he/she played and in this case If I add a movie first, the same problem still occurs since I need actors. I am new to database operations and nhibernate if the answer is obvious I am sorry.
class Program
{
static void Main(string[] args)
{
using var session = Demo.OpenSession();
using var transaction = session.BeginTransaction();
Actor actor = new Actor();
transaction.Commit();
}
}
Upvotes: 0
Views: 40
Reputation: 126
You can try something like this:
Create Movie object
Movie killBill = new Movie {
Name = "Kill Bill"
};
Create Actor object
Actor umaThurman = new Actor
{
Name = "Uma Thurman",
};
In your actor object Add to the Movies the movie object which has been created:
umaThurman.Movies.Add(killBill);
In your movie object Add to the Actors the actor object which has been created:
killBill.Actors.Add(umaThurman);
And again with the next actor:
Actor davidCarradine = new Actor
{
Name = "David Carradine",
};
davidCarradine.Movies.Add(killBill);
killBill.Actors.Add(davidCarradine);
Just use the session's save method passing the killBill movie object (save once and the NHibernate will do all, it will save killBill movie object and you actors associated in both ways: Movies with actors and actors with movies):
session.Save(killBill);
Then commit: transaction.Commit();
PS.: Don't forget to initialize your Lists in Actor and Movie classes:
// Actor Class
public virtual IList<Movie> Movies { get; set; } = new List<Movie>();
// Movie Class
public virtual IList<Actor> Actors { get; set; } = new List<Actor>();
Upvotes: 1