I am looking for a way to add a relationship between two objects and have a custom identifier for a foreign key. I have looked through previous posts, but the closest I can find is a suggestion. Charge the association - and that's not what I hope for. I know that this can be done in the Entity Framework with a .HasForeignKey binding, but I cannot find a way to do this in Fluent NHibernate.
Take two sample objects:
public class Ticket { public virtual int Id { get; set; } public virtual string Title { get; set; } public virtual string ServiceId { get; set; } public virtual Service Service { get; set; } } public class Service { public virtual string Id { get; set; } }
I want to create a new Ticket instance and assign the Service to it using the following tools (suppose that the associated Service already exists in the table):
Ticket ticket = new Ticket() { Title = "Problem with MS Word", ServiceId = "Microsoft Word 2012" };
What I do not want to do is the following:
Ticket ticket = new Ticket() { Title = "Problem with MS Word", Service = Session.Load<Service>("Microsoft Word 2012") };
I have good reasons for this, and as I said, it can be done in the Entity Framework, but I am very fixated on how to achieve the same level in Fluent NHibernate. My mappings currently look like this:
public class TicketMapping : ClassMap<Ticket> { public TicketMapping() { Id(m => m.Id); Map(m => m.Title).Column("Title"); Map(m => m.ServiceId).Column("ServiceId"); HasOne(m => m.Service).ForeignKey("ServiceId"); Schema("dbo"); Table("Tickets"); } } public class ServiceMapping : ClassMap<Service> { public ServiceMapping() { Id(m => m.Id); Schema("dbo"); Table("Services"); } }
Any help is always appreciated!
Just quick editing for Jay is the reason I don't want to use Session.Load because I don't want my view level (MVC 3) to know anything about NHibernate - so I use the repository template and injecting one repository into the controller . So, for example, I will have a TicketRepository that adheres to the following contract
public interface IRepository<T> { T GetById(object id); void Create(T entity); void Update(T entity); void Delete(T entity); }
I do not want to enter a ServiceRepository also to get a link to a Service for Ticket .. p>