I am writing mapping for the class with Fluent Nhibernate:
public class UniqueFeaturesSet : IEntity
{
public UniqueFeaturesSet(List<List<double>> mfcc)
{
MFCC = mfcc;
}
public virtual List<List<double>> MFCC { get; set; }
public virtual int Id { get; set; }
}
How to map List<List<double>> ?
It is not possible to map nested collections with [Fluent][N]Hibernate. The inner collection needs to be in an own class.
Related
Hi.
I want just map entity to dto but profile props map to the dto.
Now what can i do for the following problem?
public abstract class DtoProfile<TEntity, TDto> : Profile, IProfile
{
public DtoProfile()
{
var profile = CreateMap<TEntity, TDto>();
CustomMapping(profile);
}
public virtual void CustomMapping(IMappingExpression<TEntity, TDto> mapping)
{
}
}
public class UserDto : DtoProfile<Users,UserDto>
{
public int Id { get; set; }
public string FullName { get; set; }
public string NationalCode { get; set; }
public DateTime CreatedDate { get; set; }
}
You're doing this in a wrong way. Mapping Profiles should be for mapping, and not to be inherited from the Dto itself.
You're inherting from Profile in DtoProfile, and these properties are defined in Profile, so they will be there. And as the name of class, it's for mapping profile, and shouldn't be used for Dto.
You should have two classes, one is UserDto where you add all the properties you need to map, and the other is UserDtoProfile which inherits from DtoProfile<User, UserDto> and apply your mappings.
I have the following database tables defined:
Club: Id, Name
Member: Id, Name
ClubMember: ClubId, MemberId
I have the following entity Classes defined:
public class Club() {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Member> Members { get; set; }
}
public class Member() {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Club> Clubs { get; set; }
}
I have the following overrides defined:
public class MemberOverride : IAutoMappingOverride<Member>
{
public void Override(AutoMapping<Member> mapping_)
{
mapping_
.HasManyToMany(x_ => x_.Clubs)
.ParentKeyColumn("MemberId")
.ChildKeyColumn("ClubId")
.Cascade.All()
.Table("ClubMembers");
}
}
public class ClubOverride : IAutoMappingOverride<Club>
{
public void Override(AutoMapping<Club> mapping_)
{
mapping_
.HasManyToMany(x_ => x_.Members)
.ParentKeyColumn("ClubId")
.ChildKeyColumn("MemberId")
.Inverse()
.Table("ClubMembers");
}
}
I can see from my overrides that the Inverse on the ClubOverride means you cannot do the following
session.Save(club.Members.Add(member));
but this works:
session.Save(member.Clubs.Add(club);
But it doesn't make logical sense. I want to be able to save either the club with members or member with clubs.
Am I trying to do something impossible with FluentNhibernate?
TIA
Yes, you're right, that's not possible. But it's not a question of FluentNhibernate, NHibernate works like that.
Only one side is the owner of the relation and on charge of adding elements.
From official documentation:
Changes made only to the inverse end of the association are not persisted. This means that NHibernate has two representations in memory for every bidirectional association, one link from A to B and another link from B to A. This is easier to understand if you think about the .NET object model and how we create a many-to-many relationship in C#:
You can create add or remove methods on your entities that will help accomplish this:
public class Club() {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
private IList<Member> members;
public virtual IEnumerable<Member> Members { get { return members.Select(x => x); } }
public Club() {
members = new List<Member>();
}
public virtual void AddMember(Member member){
if (members.Contains(member))
return;
members.Add(user);
member.AddClub(this);
}
public virtual void RemoveMember(Member member){
if (!members.Contains(member))
return;
members.Remove(member);
member.RemoveClub(this);
}
}
My class:
[PersistClass]
public class ExternalAccount
{
public virtual AccountType AccountType { get; set; }
public virtual int Id { get; private set; }
public virtual User User { get; set; }
public virtual Dictionary<string, string> Parameters { get; set; }
public ExternalAccount()
{
Parameters = new Dictionary<string, string>();
}
}
The Dictionary is not getting mapped. I understand that automapping doesn't work by default with Dictionaries, how do I configure the mapping? All Parameters is is a list of key/value pairs - so I would expect them to be stored in a table with a foreign key to the externalaccount table. I know I can do this with another class - but it makes access to the parameters in the class more difficult - I'd rather have to configure the complexity once.
Please bear in mind I am new Fluent and to nHibernate.
Thanks
Using a simple class relationship such as the following:
public class Foo {
public virtual IDictionary<string, Bar> Bars { get; set; }
}
public class Bar {
public virtual string Type { get; set; }
public virtual int Value { get; set; }
}
You can map this with Fluent NHibernate in this way:
mapping.HasMany(x => x.Bars)
.AsMap(x => x.Type);
Where Bar.Type is used as the index field into the dictionary.
FluentNHibernate mapping for Dictionary
I have the following situation with fluent nhibernate:
public class Stuff
{
public Stuff()
{
Entities = new List<Entity>();
}
public virtual int Id { get; set; }
public virtual IList<Entity> Entities { get; set; }
}
public abstract class Entity
{
public virtual int Id { get; set; }
public virtual string Type { get; set; }
public virtual Stuff Stuff { get; set; }
}
public class Person : Entity
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
public class Animal : Entity
{
public virtual string Species { get; set; }
}
And then, i have the following code to use automap and generate these mappings:
var sessionFactory =
Fluently.Configure().Database(persistenceConfigurer).Mappings(
m =>
m.AutoMappings.Add(
AutoMap.Source(new Types(typeof(Entity), typeof(Person), typeof(Animal), typeof(Stuff))))
.ExportTo(#"e:\")).ExposeConfiguration(BuildSchema).BuildSessionFactory();
however, what's happening is that i get the following exception:
---> NHibernate.MappingException: Association references unmapped class: ConsoleApplication1.Models.Entity
if i make the entity class non abstract this works, however, i'd like to avoid having that table in the database but still maintain the hierarchy concept with the re
You need to add your auto mappings like this
AutoMap.AssemblyOf<Entity>(yourConfiguration).IgnoreBase<Entity>();
Not only will this ignore your Entity base class but you don't need to add each entity manually so long as each model inherits from Entity.
How should the following mapping configuration be solved?
public abstract class RepositoryEntity
{
public virtual int Id { get; set; }
}
public class Descriptor : RepositoryEntity
{
public virtual String Name { get; set; }
public virtual DateTime Timestamp { get; set; }
}
public class Proxy<TDescriptor> : RepositoryEntity
{
public virtual TDescriptor Descriptor { get; set; }
public virtual Byte[] SerializedValue { get; set; }
};
public class TestUnit : Proxy<Descriptor>
{
};
I receive problems when testing the TestUnit mapping - it says it's impossible to map the item with generic parameters. This happens if I attempt to map every class from the specified before.
If I attempt to map everything, except Proxy<T>, then I receive that there is no persister for the 'TestUnit'.
If I stop inheriting TestUnit from Proxy<Descriptor>, the mapping test works fine.
Does Fluent NHibernate have possibility to automap types inherited from some concrete Class<T> template? Could you help me with mapping these entities?
I used a combination of Fluent and Auto mappings.
Fluent mappings should be used for generics.
Configuration = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
.Mappings(x =>
{
x.FluentMappings.AddFromAssemblyOf<RepositoryEntity>();
x.AutoMappings.Add(autoPersistenceModel);
});