I have just started working on a project using Fluent NHibernate.
What is the correct way to map the following classes using Fluent NHibernate?
public class DurationUnit
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Duration
{
public virtual int Value { get; set; }
public virtual DurationUnit Unit { get; set; }
public virtual int DurationInMinutes { get{ throw new NotImplementedException(); } }
}
public class Event
{
public virtual int Id { get; set; }
public virtual String Name { get; set; }
public virtual Duration MaxDuration { get; set; }
public virtual Duration MinDuration { get; set; }
}
My inital approach was to declare a ClassMap for DurationUnit and Event, with Duration as a component of Event. When trying this I received an exception:
NHibernate.MappingException: Could not determine type for:
Entities.DurationUnit
if your mapping looks like this
public EventMap()
{
Component(x => x.MaxDuration, c =>
{
c.Map(x => x.Value, "MaxDurationValue");
c.Reference(x => x.Unit, "MaxDurationUnitId");
});
}
then make sure class DurationUnitMap is public and is added to the configuration
Related
I am trying to map my domain entity to DTO. What I am getting in generated query is wrongly concatenated property name.
This is my entity class: (some code is removed for brevity)
public class Product : BaseEntity
{
public int ProductId { get; set; }
public string Name { get; set; }
public virtual EntityUnit EntityUnit { get; set; }
}
This my DTO
public class ProductDto : IMapFrom<Product>
{
public int Id { get; set; }
public string Unit { get; set; }
public void Mapping(Profile profile)
{
profile.CreateMap<Product, ProductDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.EntityUnit.Name));
}
}
This is my EntityUnit class:
public class EntityUnit : BaseEntity
{
public int UnitId { get; set; }
public string Name { get; set; }
}
After all this is the generated query:(on mini-profiler)
Actually that must be p.UnitId instead of EntityUnitUnitId (which works). Automapper version is 9.0
What I am doing wrong here?
I have two tables "recall" and "service" and I need many to many between them.
I use fluent NHibernate mapping but it creates additional table with name "servicetorecall"
public class Recall : BaseDomain
{
public virtual string Name { get; set; }
public virtual string PersonPosition { get; set; }
public virtual string RecallText { get; set; }
private ICollection<Service> _services = new List<Service>();
public virtual ICollection<Service> Services
{
get { return _services; }
set { _services = value; }
}
}
public class Service : BaseDomain
{
public virtual string Name { get; set; }
public virtual string Url { get; set; }
public virtual string ImgPath { get; set; }
public virtual string ShortContent { get; set; }
public virtual string Content { get; set; }
public virtual bool ServiceIsVisible { get; set; }
ICollection<Recall> _recalls = new List<Recall>();
public virtual ICollection<Recall> Recalls
{
get { return _recalls; }
set { _recalls = value; }
}
}
Mappings :
class RecallMappingOverride : IAutoMappingOverride<Recall>
{
public void Override(AutoMapping<Recall> mapping)
{
mapping.Cache.ReadWrite();
mapping.HasManyToMany(q => q.Services).Table(MappingNames.RECALLS_RELATIONS)
.ParentKeyColumn(MappingNames.RECALL_ID)
.ChildKeyColumn(MappingNames.SERVICE_ID).Inverse().Cascade.All();
}
}
public class ServiceMappingOverride : IAutoMappingOverride<Service>
{
public void Override(AutoMapping<Service> mapping)
{
mapping.Cache.ReadWrite();
mapping.HasManyToMany(q => q.Recalls).Table(MappingNames.RECALLS_RELATIONS) .ParentKeyColumn(MappingNames.SERVICE_ID).ChildKeyColumn(MappingNames.RECALL_ID)
.Inverse().Cascade.All();
}
}
I tried to change cascades but this didn't help. Also I did the same with other entities and it works correctly what type of magic is it?
How do you define "correct", what do you want to achieve?
I never heard of any clean solution for many to many relations which doesn't use pivot table.
[quick glimpse at your mappings]: only one of the "ManyToMany" should be Inverse
I am trying to do this:
Hibernate: Parent-Child Relationship to Itself
but in ASP.NET MVC 4
here are my 2 models (which i have no clue if this is the correct way to do this):
public class Group
{
public int GroupID { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public virtual ICollection<GroupRelation> GroupRelations { get; set; }
}
public class GroupRelation
{
public int GroupRelationID { get; set; }
public int? ParentID { get; set; }
public int? ChildID { get; set; }
[ForeignKey("ParentID")]
public virtual Group ParentGroups { get; set; }
[ForeignKey("ChildID")]
public virtual Group ChildGroups { get; set; }
}
Here is my Context(again no clue if this is right):
public class TaskTrackerContext : DbContext
{
public DbSet<Group> Groups { get; set; }
public DbSet<GroupRelation> GroupRelations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>()
.HasMany(g => g.GroupRelations).WithOptional(g => g.ChildGroups).HasForeignKey(g => g.ChildID);
modelBuilder.Entity<Group>()
.HasMany(g => g.GroupRelations).WithOptional(g => g.ParentGroups).HasForeignKey(g => g.ParentID);
}
}
With this setup i get the following in my database:
(i tried to put in a pic or a link to a pic and it wont let me...)
So it makes the 2 relationships i am expecting "PK_GroupID - FK_ParentID" and "PK_GroupID - FK_ChildID", but then it creates an extra column called "Group_GroupID" and makes the following relationship: "PK_GroupID - FK_Group_GroupID".
So what am I doing wrong?
I have an EntityBase class for FluentNHibernate:
public abstract class EntityBase<T>
{
public EntityBase()
{
}
public static T GetById(int id)
{
return (T)Hibernate.Session.Get<T>(id);
}
public virtual void Save()
{
using (var transaction = Hibernate.Session.BeginTransaction())
{
Hibernate.Session.SaveOrUpdate(this);
transaction.Commit();
}
}
public static IList<T> List()
{
return Hibernate.Session.CreateCriteria(typeof(T)).List<T>();
}
public static IList<T> ListTop(int i)
{
return Hibernate.Session.CreateCriteria(typeof(T)).SetMaxResults(i).List<T>();
}
public virtual void Delete()
{
using (var transaction = Hibernate.Session.BeginTransaction())
{
Hibernate.Session.Delete(this);
transaction.Commit();
}
}
}
I have a base member class also a table in database:
abstract public class BaseMember:EntityBase<BaseMember>
{
public virtual int Id { get; set; }
public virtual string Email { get; set; }
public virtual string Password { get; set; }
public virtual string RecordDate { get; protected set; }
public BaseMember()
{
}
}
I have another Member class that is deriving from BaseMember class:
public class IndividualMember : BaseMember
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string PhoneNumber { get; set; }
public virtual string MobilePhoneNumber { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual bool Gender { get; set; }
public virtual string ProfileImage { get; set; }
public virtual string AddressDefinition { get; set; }
public virtual string ZipCode { get; set; }
public virtual DateTime RecordDate { get; set; }
public IndividualMember()
{
}
}
How can I map those classes with BaseMember and IndividualMember tables in db?
There are different types of Inheritance mapping strategies in Fluent NHibernate.
You can use SubclassMap mapping for derived class.
Strategies : Table-per-class-hierarchy, Table-per-subclass and Table Per Concrete Class.
For table-per-class-hierarchy strategy, you just need to specify the discriminator column.
For more reference :
http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat
https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#wiki-subclasses
I'm using ASP.NET MVC with NHibernate and Fluent.NHibernate Maps.
I would like to know how to map the classes on Fluent and to create the database tables on my MySQL:
public class AgenteDeViagem {
public virtual int Id { get; set; }
public virtual string Email { get; set; }
public virtual AgentePessoa AgentePessoa { get; set; }
}
public interface AgentePessoa {
}
public class AgenteDeViagemPJ:AgentePessoa {
public virtual int Id { get; set; }
public virtual AgenteDeViagem AgenteDeViagem { get; set; }
public virtual string Razao { get; set; }
}
public class AgenteDeViagemPF:AgentePessoa {
public virtual int Id { get; set; }
public virtual AgenteDeViagem AgenteDeViagem { get; set; }
public virtual string Nome { get; set; }
}
Thank you very much!
Looks to me like you're halfway there. You're already using virtual and relations are set, so using the Automapping strategy, you only need to build the session factory:
private static ISessionFactory InitializeNHibernate()
{
var cfg = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(c =>
c.Database("agente").Server("localhost")
.Username("user").Password("password")))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AgenteDeViagem>())
.ExposeConfiguration(configuration =>
{
// Comment to disable schema generation
BuildDatabaseSchema(configuration);
});
return cfg.BuildSessionFactory;
}
private static void BuildDatabaseSchema(Configuration configuration)
{
var schemaExport = new SchemaExport(configuration);
schemaExport.SetOutputFile("mysql_script.sql");
schemaExport.Create(false, true);
}