Working with Embeddable in FluentNHibernate? - c#

I'm looking for any how to or documentation about use of Embeddable with FluentNHibernate exactly with in the Hibernate. Does has any way to works with Embeddable in FluentNHibernate, if not has what's the best way to simulate this ?

Finally I found some solution. It's very simple like Embeddable of JPA/Hibernate.
Found here: https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#components
Then I did.
public class Cliente {
public virtual long id { set; get; }
public virtual long codigo { set; get; }
public virtual String nome { set; get; }
public virtual String sexo { set; get; }
public virtual String cpf { set; get; }
public virtual String rg { set; get; }
public virtual DateTime dtNascimento { set; get; }
public virtual Endereco endereco { set; get; } //Embeddable
public Cliente() { }
}
public class Endereco {
public String endereco;
public String numero;
public String bairro;
public String complemento;
public String cidade;
public String cep;
public EstadosBrasil uf;
public Endereco() {
}
}
Mapping
public class ClienteMap : ClassMap<Cliente> {
public ClienteMap() {
Table("CLIENTE");
Id(c => c.id).GeneratedBy.Native();
Map(c => c.codigo);
Map(c => c.nome);
Map(c => c.sexo).Length(2);
Map(c => c.cpf);
Map(c => c.rg);
Map(c => c.dtNascimento).CustomType<DateTime>();
//embeddable
Component(c => c.endereco, e => {
e.Map(c => c.endereco);
e.Map(c => c.numero).CustomType<int>();
e.Map(c => c.bairro);
e.Map(c => c.complemento);
e.Map(c => c.cidade);
e.Map(c => c.cep);
e.Map(c => c.uf).CustomType<GenericEnumMapper<EstadosBrasil>>();
});
}
}

Related

AutoMapper, the reverse map not working for embedded objects?

I have a class Affaire, containing Secteurs, containing associated Emplacements, and corresponding DTO objects to transform. I use AutoMapper for that.
public class Affaire
{
public string Nom { get; set; }
public class Emplacement
{
public int AffaireId { get; set; }
public Affaire Affaire { get; set; }
public class EmplacementDTO
{
public int AffaireId { get; set; }
public string AffaireNom { get; set; }
The Emplacements are grouped in Secteur, having as well its Affaire
public class Secteur
{
public int AffaireId { get; set; }
public Affaire Affaire { get; set; }
public IList<Emplacement> Emplacements { get; set; } = new List<Emplacement>();
When I map from Emplacement to a EmplacementDTO I don't want to include Affaire, only AffaireId:
CreateMap<Emplacement, EmplacementDTO>()
.ForMember(dto => dto.AffaireId, o => o.MapFrom(dto => dto.Affaire.Id))
.ForMember(dto => dto.AffaireNom, o => o.MapFrom(dto => dto.Affaire.Nom))
.ReverseMap()
.ForPath(bo => bo.Affaire, o => o.MapFrom(dto => (Affaire)null)); // <<<< HERE
This configuration does not seem to work when Emplacement is embedded in a container SecteurDTO, however, for the container itself, the configurartion seem to work (its Affaire is set to null):
CreateMap<Secteur, SecteurDTO>()
.ForMember(dto => dto.AffaireId, o => o.MapFrom(dto => dto.Affaire.Id))
.ForMember(dto => dto.AffaireNom, o => o.MapFrom(dto => dto.Affaire.Nom))
.ReverseMap()
.ForPath(bo => bo.Affaire, o => o.MapFrom(dto => (Affaire)null))
related problem here
Here is the working demo in my project:
Model:
public class Affaire
{
public int Id { get; set; }
public string Nom { get; set; }
public IList<Emplacement> Emplacements { get; set; } = new List<Emplacement>();
}
public class Emplacement
{
public int AffaireId { get; set; }
public Affaire Affaire { get; set; }
}
public class Secteur
{
public int AffaireId { get; set; }
public Affaire Affaire { get; set; }
public IList<Emplacement> Emplacements { get; set; } = new List<Emplacement>();
}
DTO:
public class EmplacementDTO
{
public int AffaireId { get; set; }
public string AffaireNom { get; set; }
public Affaire Affaire { get; set; }
}
public class SecteurDTO
{
public int AffaireId { get; set; }
public string AffaireNom { get; set; }
public Affaire Affaire { get; set; }
public IList<EmplacementDTO> Emplacements { get; set; }
}
Profile:
public class YourProfile : Profile
{
public YourProfile()
{
CreateMap<Emplacement, EmplacementDTO>()
.ForMember(dto => dto.AffaireId, o => o.MapFrom(dto => dto.Affaire.Id))
.ForMember(dto => dto.AffaireNom, o => o.MapFrom(dto => dto.Affaire.Nom))
.ReverseMap()
.ForPath(bo => bo.Affaire, o => o.MapFrom(dto => (Affaire)null));
CreateMap<Secteur, SecteurDTO>()
.ForMember(dto => dto.AffaireId, o => o.MapFrom(dto => dto.Affaire.Id))
.ForMember(dto => dto.AffaireNom, o => o.MapFrom(dto => dto.Affaire.Nom))
.ReverseMap()
.ForPath(bo => bo.Affaire, o => o.MapFrom(dto => (Affaire)null));
}
}
Backend testing code:
public List<EmployeeModel> Index(List<SecteurDTO> dto)
{
dto = new List<SecteurDTO>()
{
new SecteurDTO(){
AffaireId=1,
AffaireNom="aaa",
Emplacements = new List<EmplacementDTO>()
{
new EmplacementDTO(){AffaireId=1,Affaire=new Affaire(){Id=1,Nom="aa"}}
},
Affaire=new Affaire(){
Id=1,
Nom="aaa",
Emplacements=new List<Emplacement>()
{
new Emplacement(){AffaireId=1,Affaire=new Affaire(){Id=1,Nom="aa"}}
}
}
}
};
var bo= new List<Secteur>() { };
var mapper = _mapper.Map(dto, bo);
//...
}
Result:

Mapping to nested value Automapper

I'm struggling to map 2 objects. Basically have Product which is my EF model, and I'm mapping this to ProductDto, which has FileDto.
I'd like to map Product.FileName to ProductDto.File.Internal name, how to do this?
Classes below.
public class Product : BaseEntity<long>
{
[MaxLength(100)]
public string Name { get; set; }
[MaxLength(100)]
public string Barcode { get; set; }
public int ShelfLife { get; set; }
public int Weight { get; set; }
public bool HasAllergens { get; set; }
[MaxLength(100)]
public string FileName { get; set; }
[ForeignKey("Id")]
public int CustomerId { get; set; }
public virtual ICollection<ProductIngredient> ProductIngredient { get; set; }
public virtual ICollection<Nutrition> Nutritions { get; set; }
public virtual ICollection<ProductComposition> Composition { get; set; }
public virtual IList<ProductionProcess> ProductionProcess { get; set; }
}
public class ProductDto
{
public long Id { get; set; }
public DateTime CretedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public string Name { get; set; }
public string Barcode { get; set; }
public int ShelfLife { get; set; }
public int Weight { get; set; }
public bool HasAllergens { get; set; }
public int CustomerId { get; set; }
public FileDto File { get; set; }
public IList<IngredientDto> Ingredients { get; set; }
public IList<NutritionDto> Nutritions { get; set; }
public IList<ProductCompositionDto> Composition { get; set; }
public IList<ProductionProcessDto> ProductionProcess { get; set; }
}
public class ProductionProcessDto
{
public string Key { get; set; }
public string Value { get; set; }
public FileDto File { get; set; }
}
public class NutritionDto
{
public string Key { get; set; }
public string Value { get; set; }
}
public class ProductCompositionDto
{
public string Key { get; set; }
public string Value { get; set; }
}
File Dto:
public class FileDto
{
public string Base64EncodedFile { get; set; }
public string OriginalName { get; set; }
public string InternalName { get; set; }
public string Type { get; set; }
}
Automapper so far:
//Product
CreateMap<Nutrition, NutritionDto>().ReverseMap();
CreateMap<ProductComposition, ProductCompositionDto>().ReverseMap();
CreateMap<ProductionProcessDto, ProductionProcess>()
.ForMember(dest => dest.FileInternalName, opt => opt.MapFrom(src => src.File.InternalName))
.ForMember(dest => dest.FileOriginalName, opt => opt.MapFrom(src => src.File.OriginalName))
.ReverseMap();
CreateMap<Product, ProductDto>()
.ForMember(d => d.File, o => o.MapFrom(s => Mapper.Map<Product, FileDto>(s)))
.ForMember(d => d.Nutritions, o => o.MapFrom(s => s.Nutritions))
.ForMember(d => d.Composition, o => o.MapFrom(s => s.Composition))
.ForMember(d => d.ProductionProcess, o => o.MapFrom(s => s.ProductionProcess))
.ForMember(d => d.Ingredients, o => o.MapFrom(s => s.ProductIngredient.Select(pi => pi.Ingredients)))
.ReverseMap();
CreateMap<ProductDto, Product>()
.ForMember(d => d.FileName, o => o.MapFrom(s => s.File.InternalName))
.ReverseMap();
I am able to map from ProductDto (on data post) to Product but not other way around, all help much appreciated
Thanks
This code solved my issue:
.ForMember(d => d.File, o => o.MapFrom(model => new FileDto { InternalName = model.FileName }))
Applied to:
CreateMap<Product, ProductDto>()

Automapper performance issue only when debugging in Visual Studio

In my WPF application I am using AutoMapper to map from the entities to the domain objects.
For one particular mapping I am seeing a substantial slow down when running the application with debugging (F5) versus without (CTRL + F5).
Without debugging this mapping takes < 1 second, but with debugging it takes ~14 seconds.
The domain objects (simplified for brevity):
public class CompanyModel : UpdateableModel
{
public CompanyModel() : this(true){}
public CompanyModel(bool isNewModel) : base(isNewModel)
{
Projects = new ObservableCollectionEx<ProjectModel>();
Projects.ItemPropertyChanged += (sender, args) => OnPropertyChanged("Projects");
}
public int? Id { get; set;}
public string Name { get; set; }
public string CompanyXref { get; set; }
public bool IsActive { get; set; }
public ObservableCollectionEx<ProjectModel> Projects { get; set;}
public DateTime? DateUpdated { get; set;}
}
public class ProjectModel : UpdateableModel
{
public ProjectModel() : this(true){}
public ProjectModel(bool isNewModel) : base(isNewModel)
{
Tasks = new ObservableCollectionEx<ProjectTaskModel>();
SetPropertyDefault(() => StartDate, DateTime.Today);
SetPropertyDefault(() => EndDate, DateTime.Today);
// ReSharper disable once ExplicitCallerInfoArgument
Tasks.ItemPropertyChanged += (sender, args) => OnPropertyChanged("Tasks");
}
public int? Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string BillingCurrency { get; set; }
public string ExternalXref { get; set; }
public bool IsChargeable { get; set; }
public bool IsActive { get; set; }
public int TimeTypeId { get; set; }
public ObservableCollectionEx<ProjectTaskModel> Tasks { get; set;}
public DateTime? DateUpdated { get; set; }
}
public class ProjectTaskModel : UpdateableModel
{
public ProjectTaskModel() : this(true){}
public ProjectTaskModel(bool isNewModel) : base(isNewModel)
{
SetPropertyDefault(() => StartDate, DateTime.Today);
SetPropertyDefault(() => EndDate, DateTime.Today);
}
public int? Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string ExternalXref { get; set; }
public bool IsActive { get; set; }
public DateTime? DateUpdated { get; set; }
}
The entities:
public class CompanyEntity : BaseEntity
{
public virtual string CompanyName { get; set; }
public virtual string CompanyXref { get; set; }
public virtual bool IsActive { get; set; }
public virtual IList<ProjectEntity> Projects { get; set; }
public virtual DateTime? DateUpdated { get; set; }
}
public class ProjectEntity : BaseEntity
{
public virtual string ProjectName { get; set; }
public virtual string Description { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual string ExternalXref { get; set; }
public virtual bool IsChargeable { get; set; }
public virtual bool IsActive { get; set; }
public virtual int TimeTypeId { get; set; }
public virtual string CurrencyId { get; set; }
public virtual CompanyEntity ClientCompany { get; set; }
public virtual DateTime? DateUpdated { get; set; }
public virtual IList<ProjectTaskEntity> Tasks { get; set; }
}
public class ProjectTaskEntity : BaseEntity
{
public virtual string ProjectTaskName { get; set; }
public virtual string Description { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual string ExternalXref { get; set; }
public virtual bool IsActive { get; set; }
public virtual DateTime? DateUpdated { get; set; }
public virtual ProjectEntity Project { get; set; }
}
Finally the mappings, all properties are currently explicitly mapped:
CreateMap<CompanyEntity, CompanyModel>()
.ConstructUsing((CompanyEntity src) => new CompanyModel(false))
.MapMember(dest => dest.Id, src => src.Id)
.MapMember(dest => dest.Name, src => src.CompanyName)
.MapMember(dest => dest.CompanyXref, src => src.CompanyXref)
.MapMember(dest => dest.IsActive, src => src.IsActive)
.MapMember(dest => dest.Projects, src => src.Projects)
.IgnoreMember(dest => dest.State);
CreateMap<ProjectEntity, ProjectModel>()
.ConstructUsing((ProjectEntity src) => new ProjectModel(false))
.MapMember(dest => dest.Id, src => src.Id)
.MapMember(dest => dest.Name, src => src.ProjectName)
.MapMember(dest => dest.Description, src => src.Description)
.MapMember(dest => dest.BillingCurrency, src => src.CurrencyId)
.MapMember(dest => dest.StartDate, src => src.StartDate)
.MapMember(dest => dest.EndDate, src => src.EndDate)
.MapMember(dest => dest.ExternalXref, src => src.ExternalXref)
.MapMember(dest => dest.IsActive, src => src.IsActive)
.MapMember(dest => dest.IsChargeable, src => src.IsChargeable)
.MapMember(dest => dest.Tasks, src => src.Tasks)
.MapMember(dest => dest.TimeTypeId, src => src.TimeTypeId)
.IgnoreMember(dest => dest.State);
CreateMap<ProjectTaskEntity, ProjectTaskModel>()
.ConstructUsing((ProjectTaskEntity src) => new ProjectTaskModel(false))
.MapMember(dest => dest.Id, src => src.Id)
.MapMember(dest => dest.Name, src => src.ProjectTaskName)
.MapMember(dest => dest.Description, src => src.Description)
.MapMember(dest => dest.StartDate, src => src.StartDate)
.MapMember(dest => dest.EndDate, src => src.EndDate)
.MapMember(dest => dest.ExternalXref, src => src.ExternalXref)
.MapMember(dest => dest.IsActive, src => src.IsActive)
.IgnoreMember(dest => dest.State);
When the mapping is performed there are 16 companies, 122 total projects, and each project only has one task currently.
I did try ignoring various properties on the mappings and it seems to be the Tasks collection at the project level that is causing this; with it ignored the mapping takes 2 seconds instead of 14.
Is there something about how Visual Studio hosts the application for debugging that could cause this performance issue?
You could possibly download an alternate CSharp IDE run it in debugging mode there and see if there is a difference.
SharpDev is an opensource IDE that is very nice and supports C# projects.
http://www.icsharpcode.net/OpenSource/SD/Download/
It would at least be interesting and a simple way to try an alternative.
SharpDev also has a code profiler built-in which may indicate what the issue is.
Good luck.

Child-parent query with composite id on child doing unnecessary multiple queries

I have a very strange problem with this context.
This is the parent object (order heading, IEntity is an empty interface):
public class OrderEntity : IEntity
{
public virtual int prg_ordine { get; set; }
public virtual int num_anno { get; set; }
public virtual int num_doc { get; set; }
public virtual String num_doc_esteso { get; set; }
public virtual DateTime? dat_doc { get; set; }
public virtual String cod_clifor_c { get; set; }
public virtual String rag_soc { get; set; }
public virtual DateTime? dat_evasione { get; set; }
public virtual decimal qta_peso_lordo_kg { get; set; }
public virtual decimal qta_peso_netto_kg { get; set; }
public virtual decimal qta_totale { get; set; }
public virtual String ind_sped { get; set; }
public virtual String ind_sped_div { get; set; }
public virtual String rag_soc_div { get; set; }
public virtual String cod_agente { get; set; }
public virtual String des_agente { get; set; }
public virtual String cod_pag { get; set; }
public virtual String des_pag { get; set; }
public virtual String ind_stato_evas { get; set; }
public virtual int cod_commessa_num { get; set; }
public virtual String des_num_esterno { get; set; }
public virtual ClientEntity client { get; set; }
public virtual ICollection<OrderItemEntity> orderItems { get; set; }
}
This is the child object (order rows):
public class OrderItemEntity : IEntity
{
public virtual int prg_ordine { get; set; }
public virtual int prg_ordine_riga { get; set; }
public virtual String cod_art_completo { get; set; }
public virtual String cod_clifor_c { get; set; }
public virtual String rag_soc { get; set; }
public virtual int num_doc { get; set; }
public virtual String num_doc_esteso { get; set; }
public virtual DateTime? dat_doc { get; set; }
public virtual String cod_art { get; set; }
public virtual String tipo_art { get; set; }
public virtual int prg_art { get; set; }
public virtual String ind_tiporiga { get; set; }
public virtual String cod_um_doc { get; set; }
public virtual String des_articolo_riga { get; set; }
public virtual decimal qta_daevadere { get; set; }
public virtual decimal qta_evasa { get; set; }
public virtual decimal qta_ordine { get; set; }
public virtual DateTime? dat_evasione { get; set; }
public virtual DateTime? dat_evas_riga { get; set; }
public virtual String note { get; set; }
public virtual bool gestisci_pezzatura { get; set; }
public virtual int numero_pezzi { get; set; }
public virtual decimal quantita_pezzo { get; set; }
public virtual string commessa { get; set; }
public virtual int num_riga { get; set; }
public virtual ArticoloDisponibilitaEntity articoloDisponibilita { get; set; }
public virtual OrderEntity order { get; set; }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
These are the two other objects referred:
public class ClientEntity : IEntity
{
public virtual String cod_clifor { get; set; }
public virtual String des_ragsoc { get; set; }
public virtual String indirizzo { get; set; }
public virtual String codice_e_descrizione
{
get
{
return cod_clifor + " - " + des_ragsoc;
}
}
public virtual ICollection<OrderEntity> orders { get; set; }
}
public class ArticoloDisponibilitaEntity :IEntity
{
public virtual String cod_art_completo { get; set; }
public virtual decimal qta_esistente { get; set; }
public virtual decimal qta_in_distinte_lavorazione { get; set; }
public virtual decimal qta_in_distinte_aperte { get; set; }
public virtual decimal qta_libera_alla_vendita()
{
var ret = qta_esistente - qta_in_distinte_aperte - qta_in_distinte_lavorazione;
if(ret < 0)
ret = 0;
return ret;
}
}
Here are the Fluent nHibernate mapping classes:
public class OrderEntityMap : ClassMap<OrderEntity>
{
public OrderEntityMap()
{
this.Id(x => x.prg_ordine);
this.Map(x => x.num_anno);
this.Map(x => x.num_doc);
this.Map(x => x.num_doc_esteso);
this.Map(x => x.dat_doc);
this.Map(x => x.cod_clifor_c);
this.Map(x => x.rag_soc);
this.Map(x => x.dat_evasione);
this.Map(x => x.qta_peso_lordo_kg);
this.Map(x => x.qta_peso_netto_kg);
this.Map(x => x.qta_totale);
this.Map(x => x.ind_sped);
this.Map(x => x.ind_sped_div);
this.Map(x => x.rag_soc_div);
this.Map(x => x.cod_agente);
this.Map(x => x.des_agente);
this.Map(x => x.cod_pag);
this.Map(x => x.des_pag);
this.Map(x => x.ind_stato_evas);
this.Map(x => x.cod_commessa_num);
this.Map(x => x.des_num_esterno);
References<ClientEntity>(x => x.client, "cod_clifor_c").Not.Nullable();
this.HasMany<OrderItemEntity>(x => x.orderItems).KeyColumn("prg_ordine").Not.LazyLoad().Cascade.All();
this.Table("VW_E_OrdiniTestate");
this.ReadOnly();
}
}
public class OrderItemEntityMap : ClassMap<OrderItemEntity>
{
public OrderItemEntityMap()
{
this.CompositeId()
.KeyProperty(x => x.prg_ordine, "prg_ordine_riga")
.KeyReference(x => x.order, "prg_ordine");
this.Map(x => x.prg_ordine );
this.Map(x => x.prg_ordine_riga );
this.Map(x => x.cod_art_completo );
this.Map(x => x.cod_clifor_c );
this.Map(x => x.rag_soc );
this.Map(x => x.num_doc );
this.Map(x => x.num_doc_esteso );
this.Map(x => x.dat_doc );
this.Map(x => x.cod_art );
this.Map(x => x.tipo_art );
this.Map(x => x.prg_art );
this.Map(x => x.ind_tiporiga );
this.Map(x => x.cod_um_doc );
this.Map(x => x.des_articolo_riga );
this.Map(x => x.qta_daevadere );
this.Map(x => x.qta_evasa );
this.Map(x => x.qta_ordine );
this.Map(x => x.dat_evasione );
this.Map(x => x.dat_evas_riga );
this.Map(x => x.note );
this.Map(x => x.gestisci_pezzatura );
this.Map(x => x.numero_pezzi );
this.Map(x => x.quantita_pezzo);
this.Map(x => x.commessa);
this.Map(x => x.num_riga);
References<ArticoloDisponibilitaEntity>(x => x.articoloDisponibilita, "cod_art_completo").Not.Nullable();
References<OrderEntity>(x => x.order, "prg_ordine").Not.Nullable();
this.Table("VW_E_OrdiniRighe");
this.ReadOnly();
}
}
public class ClientEntityMap : ClassMap<ClientEntity>
{
public ClientEntityMap()
{
this.Table("VW_E_Clienti");
this.Id(x => x.cod_clifor);
this.Map(x=> x.des_ragsoc);
this.Map(x => x.indirizzo);
this.HasMany(x => x.orders).KeyColumn("cod_clifor_c");
}
}
public class ArticoloDisponibilitaEntityMap : ClassMap<ArticoloDisponibilitaEntity>
{
public ArticoloDisponibilitaEntityMap()
{
this.Table("VW_E_Articoli_Disp");
this.Id(x => x.cod_art_completo);
this.Map(x => x.qta_esistente);
this.Map(x => x.qta_in_distinte_aperte);
this.Map(x => x.qta_in_distinte_lavorazione);
this.ReadOnly();
}
}
This query:
return All().FetchMany(x => x.orderItems).Fetch(x => x.client).Where(x => x.cod_clifor_c == filters.clientId).ToList();
Causes this query to be executed:
select orderentit0_.prg_ordine as prg1_11_0_,
orderitems1_.prg_ordine_riga as prg1_12_1_,
orderitems1_.prg_ordine as prg2_12_1_,
cliententi2_.cod_clifor as cod1_3_2_,
orderentit0_.num_anno as num2_11_0_,
orderentit0_.num_doc as num3_11_0_,
orderentit0_.num_doc_esteso as num4_11_0_,
orderentit0_.dat_doc as dat5_11_0_,
orderentit0_.cod_clifor_c as cod6_11_0_,
orderentit0_.rag_soc as rag7_11_0_,
orderentit0_.dat_evasione as dat8_11_0_,
orderentit0_.qta_peso_lordo_kg as qta9_11_0_,
orderentit0_.qta_peso_netto_kg as qta10_11_0_,
orderentit0_.qta_totale as qta11_11_0_,
orderentit0_.ind_sped as ind12_11_0_,
orderentit0_.ind_sped_div as ind13_11_0_,
orderentit0_.rag_soc_div as rag14_11_0_,
orderentit0_.cod_agente as cod15_11_0_,
orderentit0_.des_agente as des16_11_0_,
orderentit0_.cod_pag as cod17_11_0_,
orderentit0_.des_pag as des18_11_0_,
orderentit0_.ind_stato_evas as ind19_11_0_,
orderentit0_.cod_commessa_num as cod20_11_0_,
orderentit0_.des_num_esterno as des21_11_0_,
orderitems1_.cod_art_completo as cod3_12_1_,
orderitems1_.cod_clifor_c as cod4_12_1_,
orderitems1_.rag_soc as rag5_12_1_,
orderitems1_.num_doc as num6_12_1_,
orderitems1_.num_doc_esteso as num7_12_1_,
orderitems1_.dat_doc as dat8_12_1_,
orderitems1_.cod_art as cod9_12_1_,
orderitems1_.tipo_art as tipo10_12_1_,
orderitems1_.prg_art as prg11_12_1_,
orderitems1_.ind_tiporiga as ind12_12_1_,
orderitems1_.cod_um_doc as cod13_12_1_,
orderitems1_.des_articolo_riga as des14_12_1_,
orderitems1_.qta_daevadere as qta15_12_1_,
orderitems1_.qta_evasa as qta16_12_1_,
orderitems1_.qta_ordine as qta17_12_1_,
orderitems1_.dat_evasione as dat18_12_1_,
orderitems1_.dat_evas_riga as dat19_12_1_,
orderitems1_.note as note12_1_,
orderitems1_.gestisci_pezzatura as gestisci21_12_1_,
orderitems1_.numero_pezzi as numero22_12_1_,
orderitems1_.quantita_pezzo as quantita23_12_1_,
orderitems1_.commessa as commessa12_1_,
orderitems1_.num_riga as num25_12_1_,
orderitems1_.prg_ordine as prg2_0__,
orderitems1_.prg_ordine_riga as prg1_0__,
cliententi2_.des_ragsoc as des2_3_2_,
cliententi2_.indirizzo as indirizzo3_2_
from VW_E_OrdiniTestate orderentit0_
left outer join VW_E_OrdiniRighe orderitems1_
on orderentit0_.prg_ordine = orderitems1_.prg_ordine
left outer join VW_E_Clienti cliententi2_
on orderentit0_.cod_clifor_c = cliententi2_.cod_clifor
where orderentit0_.cod_clifor_c = '000030' /* #p0 */
which is perfectly fine.
The problem is that, after the above query, one query PER ORDER ROW is then executed, like this:
SELECT orderiteme0_.prg_ordine_riga as prg1_12_0_,
orderiteme0_.prg_ordine as prg2_12_0_,
orderiteme0_.cod_art_completo as cod3_12_0_,
orderiteme0_.cod_clifor_c as cod4_12_0_,
orderiteme0_.rag_soc as rag5_12_0_,
orderiteme0_.num_doc as num6_12_0_,
orderiteme0_.num_doc_esteso as num7_12_0_,
orderiteme0_.dat_doc as dat8_12_0_,
orderiteme0_.cod_art as cod9_12_0_,
orderiteme0_.tipo_art as tipo10_12_0_,
orderiteme0_.prg_art as prg11_12_0_,
orderiteme0_.ind_tiporiga as ind12_12_0_,
orderiteme0_.cod_um_doc as cod13_12_0_,
orderiteme0_.des_articolo_riga as des14_12_0_,
orderiteme0_.qta_daevadere as qta15_12_0_,
orderiteme0_.qta_evasa as qta16_12_0_,
orderiteme0_.qta_ordine as qta17_12_0_,
orderiteme0_.dat_evasione as dat18_12_0_,
orderiteme0_.dat_evas_riga as dat19_12_0_,
orderiteme0_.note as note12_0_,
orderiteme0_.gestisci_pezzatura as gestisci21_12_0_,
orderiteme0_.numero_pezzi as numero22_12_0_,
orderiteme0_.quantita_pezzo as quantita23_12_0_,
orderiteme0_.commessa as commessa12_0_,
orderiteme0_.num_riga as num25_12_0_
FROM VW_E_OrdiniRighe orderiteme0_
WHERE orderiteme0_.prg_ordine_riga = 1 /* #p0 */
and orderiteme0_.prg_ordine = 22 /* #p1 */
This is unexpected and unwanted; I suspect something wrong in the mappings, but it seems everything good to me.
Any hint on why this happens and how to avoid it?
Thanks,
Mario
I think your problem is with the implementation of the GetHashCode method in the child entity, with Nhibernate when you have a composite id you have to tell to Nh how to reconize the entities.
Try to implement those two methods:
public override bool Equals(object obj)
{
OrderItemEntity objfrom = (OrderItemEntity)obj;
return ((this.prg_ordine == objfrom.prg_ordine) && (this.prg_ordine_riga == objfrom.prg_ordine_riga));
}
public override int GetHashCode()
{
unchecked
{
return ((this.prg_ordine * 100000) + this.prg_ordine_riga);
}
Bye
Marco

FNH Many-to-Many relationship with attributes

I have three tables as follow:
Content:
public class Content
{
public Content()
{
ContentFieldItems = new List<ContentFieldItem>();
ContentFields = new Dictionary<ContentField, ContentFieldItem>();
People = new List<Person>();
}
public virtual int id { get; set; }
public virtual int categoryid { get; set; }
public virtual int userid { get; set; }
public virtual DateTime? date { get; set; }
public virtual int status { get; set; }
public virtual IDictionary<ContentField, ContentFieldItem> ContentFields { get; set; }
public virtual IList<ContentFieldItem> ContentFieldItems { get; set; }
public virtual IList<Person> People { get; set; }
public virtual string title { get; set; }
}
public class ContentMap : ClassMap<Content>
{
Id(x => x.id);
Map(x => x.categoryid);
Map(x => x.userid);
Map(x => x.date);
Map(x => x.status);
Map(x => x.title);
HasMany<ContentFieldItem>(x=>x.ContentFields)
.Table("ContentFieldItem")
.KeyColumn("contentid")
.AsMap(f => f.ContentField)
.Component(c =>
{
c.ParentReference(m => m.Content);
c.References(m => m.ContentField);
c.Map(m => m.fieldadditionalinfo);
c.Map(m => m.fieldvalue);
})
.Inverse()
.Cascade.AllDeleteOrphan();
...
}
ContentField:
public class ContentField
{
public ContentField()
{
Contents = new List<Content>();
}
public virtual int id { get; set; }
public virtual string field { get; set; }
public virtual IList<Content> Contents { get; set; }
public virtual void AddContentField(Content content,
ContentFieldItem contentfielditem)
{
Contents.Add(content);
content.ContentFields.Add(this, contentfielditem);
}
}
public class ContentFieldMap : ClassMap<ContentField>
{
public ContentFieldMap()
{
Id(x => x.id);
Map(x => x.field);
Map(x => x.categoryid);
Map(x => x.status);
Map(x => x.type);
Map(x => x.showonall);
}
}
ContentFieldItem:
public class ContentFieldItem
{
public virtual int id { get; set; }
public virtual string fieldvalue { get; set; }
public virtual string fieldadditionalinfo { get; set; }
public virtual Content Content { get; set; }
public virtual ContentField ContentField { get; set; }
}
public class ContentFieldItemMap : ClassMap<ContentFieldItem>
{
public ContentFieldItemMap()
{
Id(x => x.id);
Map(x => x.fieldvalue);
Map(x => x.fieldadditionalinfo);
References(x => x.Content, "contentid");
References(x => x.ContentField, "fieldid");
}
}
These three tables at the same time I want to create a way of inter-related.
I want to create a ContentFieldItem with relation Content through contentid and ContentField through fieldid and some more parameters.I think this can be done with many-to-many without parameters but i have to do with parameters, how is this possible with fluent nhibernate?
and now m getting "Could not determine type for: Project.Models.ContentField".
i would do
public class Content
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual IDictionary<ContentField, ContentFieldInfo> Fields { get; set; }
}
public class ContentField
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class ContentFieldInfo
{
public virtual Content Content { get; set; }
public virtual ContentField Field { get; set; }
public virtual string Values { get; set; }
public virtual string AdditionalInfo { get; set; }
}
class ContentMap : ClassMap<Content>
{
public ContentMap()
{
Id(...);
Map(c => c.Title);
HasMany(c => c.Fields)
.Table("ContentFieldItem")
.KeyColumn("contentid")
.AsMap(fieldinfo => fieldinfo.Field)
.Component(comp =>
{
comp.ParentReference(fi => fi.Content);
comp.References(fi => fi.ContentField);
comp.Map(fi => fi.Value);
...
});
}
}
class ContentFieldMap : ClassMap<ContentField>
{
public ContentFieldMap()
{
Id(...);
Map(c => c.Name);
}
}
can you clarify what you mean with ContentField through fieldid and some more parameters.?
Hope this helps

Categories