Mapping multiple properties of a same type with HasMany via automapping
Asked Answered
N

1

10

I am trying to map properties of the same type on a OneToMany association. I tried to distinguish with Description but kinda stuck here.

public class User
{
    public virtual int UserId { get; set; }
    public virtual string UserName { get; set; }

    [Description("From")]
    public virtual IList<Message> FromMessageList { get; set; }

    [Description("To")]
    public virtual IList<Message> ToMessageList { get; set; }   
}

public class Message
{
    public virtual int MessageId { get; set; }
    public virtual string Text { get; set; }

    [Description("From")]
    public virtual User FromUser { get; set; }

    [Description("To")]
    public virtual User ToUser { get; set; }

}

    public class DefaultHasManyConvention : IHasManyConvention
    {
        public void Apply(IOneToManyCollectionInstance instance)
        {
            if (instance.OtherSide.Property.GetDescription() == instance.Member.GetDescription())
            {
                if (instance.Member.GetDescription() != null)
                    instance.Key.Column(instance.Member.GetDescription() + "Id");
                else
                    instance.Key.Column(instance.OtherSide.Property.Name + "Id");

                instance.Fetch.Select();
            }
        }
    }

    public class DefaultReferenceConvention : IReferenceConvention
    {
        public void Apply(IManyToOneInstance instance)
        {
            if (instance.Property.GetDescription() != null)
                instance.Column(instance.Property.GetDescription() + "Id");
            else
                instance.Column(instance.Property.Name + "Id");

            instance.Fetch.Select();
        }
    }
Nagana answered 5/6, 2015 at 10:38 Comment(4)
maybe I am looking for something like MappedBy in NHibernateNagana
Is this Entity Framework? Do you want simple one to many mapping or something more specific?Subterfuge
nhibernate tagged and you are asking if this is EF? probably you are not qualified to answer this.Nagana
I did not see the tag of Nhibernate. It does not show quality, it shows I just missed reading a tag :) Chill out.Subterfuge
R
1

For one to many relationships I generally use coding like :

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }

    [Description("From")]
    public virtual ICollection<Message> FromMessageList { get; set; }

    [Description("To")]
    public virtual ICollection<Message> ToMessageList { get; set; }  

}

public class Message
{
    public int MessageId { get; set; }
    public string Text { get; set; }

    [Description("From")]
    public virtual User FromUser { get; set; }
    // From user foreign key column
    [ForeignKey("FromUser")]
    public int FromUserId {get;set;}


    [Description("To")]
    public virtual User ToUser { get; set; }
    // ToUser foreign key column
    [ForeignKey("ToUser")]
    public int ToUserId {get;set;}

}
  1. Try to use ICollection instead of IList - this solved many issues for me.
  2. Add foreign key column names; it makes mapping simpler and filtering in queries easier.
Royster answered 24/3, 2016 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.