NHibernate group by parentId in child and get the value from parent table for this parentId - c#-4.0

I have one to many between Header and Detail as shown in the below class
public class Header
{
public virtual string ScriptNumber { get; set; }
public virtual IList<Detail> Details { get; set; }
public virtual string StartTime { get; set; }
}
public class Detail
{
public virtual string ScriptNumber { get; set; }
public virtual DateTime UpdatedDate { get; set; }
public virtual Header ProgramHeader { get; set; }
}
I am trying to query the Detail to group by ScriptNumber and get the corresponding StartTime from the Header table. Can anyone help me how to do this.
Currently my NHibernate query is:
var changesInDetail = _session.QueryOver<Detail>()
.Where(x => x.UpdatedDate.IsBetween(changedFrom).And(changedTo))
.SelectList(list => list.SelectGroup(pr => pr.ScriptNumber))
.Future<string>();
How to get the StartTime from Header for the grouped by value in Detail?
The following works as I expected. Can anyone please confirm if this is the best approach?
var changedScriptsInDetail1 = _session.QueryOver<Detail>(() => detailAlias)
.JoinQueryOver(d => d.ProgramHeader, () => headerAlias)
.Where(() => detailAlias.UpdatedDate.IsBetween(changedFrom).And(changedTo))
.SelectList(list => list
.SelectGroup(pr => pr.ScriptNumber)
.SelectGroup(()=>headerAlias.StartTime)
)
.List<object[]>();

var changedScriptsInDetail1 = _session.QueryOver<Detail>(() => detailAlias)
.JoinQueryOver(d => d.ProgramHeader, () => headerAlias)
.Where(() => detailAlias.UpdatedDate.IsBetween(changedFrom).And(changedTo))
.SelectList(list => list
.SelectGroup(pr => pr.ScriptNumber)
.SelectGroup(()=>headerAlias.StartTime)
)
.List<object[]>();

Related

Automapper & EF Core: One-to-many relationship exposed by DTO

I effectively have the following entities defined:
public class Order
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Description { get; set; }
public int? OrderId { get; set; }
public Order Order { get; set; }
}
They are configured so that the OrderId in Part is linked to the Order Id:
// Order Model Builder
var orderModelBuilder = modelBuilder.Entity<Order>()
.ToTable("Orders");
orderModelBuilder.HasKey(i => i.Id);
orderModelBuilder.HasMany<Part>()
.WithOne()
.HasForeignKey(i => i.OrderId);
// Part Model Builder
var partModelBuilder = modelBuilder.Entity<Part>()
.ToTable("Parts");
partModelBuilder.HasKey(i => i.Id);
partModelBuilder.HasOne(i => i.Order)
.WithMany()
.HasForeignKey(i => i.OrderId);
Now I would like to map these to a detailed order DTO which includes a collection of parts for an order:
public class PartDto
{
public int Id { get; set; }
public string Description { get; set; }
}
public class OrderDetailsDto
{
public int Id { get; set; }
public string Name { get; set; }
public List<PartDto> Parts { get; set; }
}
Currently, I use Automapper's ProjectTo() to handle queries so I can query what I want and get a list of orders back already mapped to my desired DTO. Now, I want to add Parts to that query so I can get all of the orders back with parts in one query without having to loop through orders and individually fetch the parts for each order returned. I could easily do that if List was part of the Order entity class but adding it at this stage isn't really an option.
CreateMap<Part, PartDto>()
.ForMember(d => d.Id, a => a.MapFrom(s => s.Id))
.ForMember(d => d.Description, a => a.MapFrom(s => s.Description))
;
CreateMap<Order, OrderDetailsDto>()
.ForMember(d => d.Id, a => a.MapFrom(s => s.Id))
.ForMember(d => d.Name, a => a.MapFrom(s => s.Name))
.ForMember(d => d.Parts, a => a.MapFrom(s => ?????))
;
.
..
...
await dbContext.Set<Order>().Where(...).ProjectTo<OrderDetailsDto>(configurationProvider).ToListAsync()
I know I can get shadow variables using EF.Property(object, name) but not sure how to load a restricted collection (dbContext.Set.Where(i => i.OrderId == orderId)). Any suggestions would be greatly appreciated!
--
I'm also open to adding another Entity class if there is a way to make it exist next to the existing entity. So far, I can't find a way to do that without EF Core complaining that it can only have one entity per class (without a discriminator column which would defeat the purpose!).

Automapper 8 mapping not working properly

I have two model classes, when I try to map different properties of different name by using Automapper ForMember method. It throws an automapper configuration validation exception on the mapping of different property.
I have tried a lot but It does not help.I do not know why It is throwing an exception when I try to map Quantity property with Quntity property. but when I put same name of the property in both the model classes then it works
Below is located all the model classes, exception and configurations regarding automapper.
Could you please help me, that how to solve problem?
public class ProductModel
{
public ProductModel()
{
Id = GuidContext.Current.NewGuid();
ProductHistory = new HashSet<ProductHistoryModel>();
}
public Guid Id { get; set; }
public string ProductCode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public decimal? Price { get; set; }
public int? Quntity { get; set; }
public Guid ProductCategoryId { get; set; }
public Guid? BrandId { get; set; }
public Guid ProductAttributeId { get; set; }
public virtual BrandModel Brand { get; set; }
public virtual ProductCategoryModel ProductCategory { get; set; }
public virtual ProductAttributeModel ProductAttribute { get; set; }
public virtual ICollection<ProductHistoryModel> ProductHistory { get; set; }
}
The another class is
public class ProductModel
{
public string Name { set; get; }
//public List<string> Attributes { set; get; }
//public string Brand { get; set; }
public decimal? Price
{
get; set;
}
public int? Quantity { get; set; }
}
}
and the mapping configuration is
public class ProductModelMapConfigurator : Profile, IMapConfigurator
{
public void Configure()
{
Mapper.Initialize(cfg =>
{
CreateMap<StandardizeInventory.Models.Product.ProductModel, Models.ProductModel>()
//.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Brand.Name))
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.ForMember(dest => dest.Price, opt => opt.MapFrom(src => src.Price))
.ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quntity));
//.AfterMap((src, dest) => {
// dest.Attributes = src.ProductAttribute.ProductAttributeValue.Select(x => x.Value).ToList();
//});
CreateMap<Models.ProductModel, StandardizeInventory.Models.Product.ProductModel>();
});
}
}
Below is the Exception Details
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
==========================================================================================
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
ProductModel -> ProductModel (Destination member list)
StandardizeInventory.Models.Product.ProductModel -> InventoryStoreApi.Models.ProductModel (Destination member list)
Unmapped properties:
Quantity
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) in
Any help would be appreciated. Thanks
You're using a Profile wrong, see the documentation on Profiles
Your profile should look like:
public class ProductModelMapConfigurator : Profile, IMapConfigurator
{
public ProductModelMapConfigurator()
{
CreateMap<StandardizeInventory.Models.Product.ProductModel, Models.ProductModel>()
//.ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Brand.Name))
.ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quntity));
//.AfterMap((src, dest) => {
// dest.Attributes = src.ProductAttribute.ProductAttributeValue.Select(x => x.Value).ToList();
//});
CreateMap<Models.ProductModel, StandardizeInventory.Models.Product.ProductModel>();
}
}
Get rid of that Mapper.Initialize call from inside your Profile, and change the profile to use a constructor, not whatever that Configure method is. You also don't need MapFrom when the names match, that's the "Auto" of "AutoMapper".

How to use same complex type as a property of several output types but different source types

I have a class for CountryAndCity:
public class CountryAndCity
{
public Country Country { get; set; }
public City City { get; set; }
public ZipCode ZipCode { get; set; }
}
I am using this class in several output classes, for example:
public class OutputClassA
{
public CountryAndCity CountryAndCity { get; set; }
}
public class OutputClassB
{
public CountryAndCity CountryAndCity { get; set; }
}
In addition, i have some "Input Classes" which includes the same relevant fields for mapping with additional other fields:
public class InputClassA : ICountryAndCity
{
...Some other properties.....
public int? CountryId { get; set; }
public string CountryDesc { get; set; }
public int? CityId { get; set; }
public string CityDesc { get; set; }
...Some other properties.....
}
public class InputClassB : ICountryAndCity
{
...Some other properties.....
public int? CountryId { get; set; }
public string CountryDesc { get; set; }
public int? CityId { get; set; }
public string CityDesc { get; set; }
...Some other properties.....
}
i didn't want to duplicate the code for every CreateMap statement for CountryAndCity property mapping for every Input and output combination types so i decided to use an interface that all the "input types" implements.
i created this configuration and its working great if i implement ICountryAndCity on all the relevant "input classes" and using "Mapper.Map" for the interface inside the CreateMap function.
Mapper.Initialize(cfg =>
{
cfg.CreateMap<InputClassA, OutputClassA>()
.ForMember(dest => dest.CountryAndCity,
opts => opts.MapFrom(src => Mapper.Map<ICountryAndCity, CountryAndCity>((ICountryAndCity)src)));
cfg.CreateMap<InputClassB, OutputClassB>()
.ForMember(dest => dest.CountryAndCity,
opts => opts.MapFrom(src => Mapper.Map<ICountryAndCity, CountryAndCity>((ICountryAndCity)src)));
cfg.CreateMap<ICountryAndCity, CountryAndCity>()
.ForMember(dest => dest.Country,
opts => opts.MapFrom(
src => new Country
{
CountryId = src.CountryId,
CountryDesc = src.CountryDesc
}))
.ForMember(dest => dest.City,
opts => opts.MapFrom(
src => new City
{
CityId = src.CityId,
CityDesc = src.CityDesc
}))
.ForMember(dest => dest.ZipCode,
opts => opts.MapFrom(
src => new ZipCode
{
ZipCodeId = src.ZipCodeId,
ZipCodeDesc = src.ZipCodeDesc
}));
}
I am sure that there is other better way to do it using AutoMapper without using interface. Can someone help with that?

Automapper How to devide properties from one object to properties of two objects same type

I have one object AllDriversDetails that contains data for two driver: First and Second.
public class AllDriversDetails
{
public string FirstDriverId { get; set; }
public string FirstDriverName { get; set; }
public string SecondDriverId { get; set; }
public string SecondDriverName { get; set; }
}
I have to convert AllDriversDetails to ComplexDriversDetails that has two properties of same type
public class ComplexDriversDetails
{
public DriverDetails FirstDriver { get; set; }
public DriverDetails SecondDriver { get; set; }
}
public class DriverDetails
{
public string Id { get; set; }
public string Name { get; set; }
}
Is it possible to do with automapper?
I should be no problem. Try something like this
Mapper.CreateMap<AllDriversDetails, ComplexDriversDetails>()
.ForMember(m => m.FirstDriver, opt => opt.MapFrom(src => new DriverDetails {Id = src.FirstDriverId, Name = FirstDriverName }))
.ForMember(m => m.SecondDriver , opt => opt.MapFrom(src => new DriverDetails {Id = src.SecondDriverId, Name = SecondDriverName }))
You can find more information here: http://cpratt.co/using-automapper-creating-mappings/

Entity Framework 4.0 CTP5 Many to Many relation ship with a help table?

i have a question about EF4 again :) sorry i can't find any thing on the net.
The question is i am using CTP5 Code Only to map (many to many), how do i do it???
But i am doing the old fashion way, i mean i have three tables:
Users
Companies.
UsersToCompanies -> (UserId, CompanyId)
How do i map this, it will be really appreciated if you would show me the code example to it,
from POCO's to Mapping.
This is the error i receive...
This is my entities
public class User
{
public int UserId { get; set; }
public int? UserRoleId { get; set; }
public string UserName { get; set; }
public string UserPassword { get; set; }
public DateTime InsertDate { get; set; }
public virtual UserRole UserRole { get; set; }
//this is my many to many prop
public virtual ICollection<Company> Companies { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public string CompanyNameHe { get; set; }
public string CompanyNameEn { get; set; }
public string CompanyParent { get; set; }
public DateTime InsertDate { get; set; }
//this is my many to many prop
public virtual ICollection<User> Users { get; set; }
}
/*relationship map*/
public class UsersCompanies
{
public int Id { get; set; }
public int UserId { get; set; }
public int CompanyId { get; set; }
}
//mapping
public class CompanyMap : BaseConfig<Company>
{
public CompanyMap()
{
/*Identity*/
HasKey(c => c.CompanyId);
Property(c => c.CompanyId).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("COMPANY_ID");
/*Have default values*/
Property(c => c.InsertDate).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("INSERT_DATE");
/*simple scalars*/
Property(c => c.CompanyNameHe).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_NAME_HE");
Property(c => c.CompanyNameEn).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_NAME_EN");
Property(c => c.CompanyParent).HasMaxLength(32).IsRequired().HasColumnName("COMPANY_PARENT");
ToTable("CMS_COMPANY", "GMATEST");
}
}
public class UserMap : BaseConfig<User>
{
public UserMap()
{
/*Identity*/
HasKey(c => c.UserId);
Property(c => c.UserId).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("USER_ID");
/*Have default values*/
Property(c => c.InsertDate).HasDatabaseGenerationOption(DatabaseGenerationOption.Computed).HasColumnName("INSERT_DATE");
/*simple scalars*/
Property(c => c.UserName).HasMaxLength(25).IsRequired().HasColumnName("USER_NAME");
Property(c => c.UserPassword).HasMaxLength(25).IsRequired().HasColumnName("USER_PASSWORD");
Property(c => c.UserRoleId).IsRequired().HasColumnName("USER_ROLE_ID");
/*relationship*/
HasRequired(u => u.UserRole).WithMany().HasForeignKey(t => t.UserRoleId);
HasMany(p => p.Companies).WithMany(c => c.Users).Map(mc =>
{
mc.ToTable("UsersCompanies");
mc.MapLeftKey(p => p.UserId, "CompanyId");
mc.MapRightKey(c => c.CompanyId, "UserId");
});
ToTable("CMS_USERS", "GMATEST");
}
}
public class UsersCompaniesMap : BaseConfig<UsersCompanies>
{
public UsersCompaniesMap()
{
/*Identity*/
HasKey(k => k.Id);
Property(c => c.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity).HasColumnName("ID");
Property(c => c.UserId).IsRequired().HasColumnName("USER_ID");
Property(c => c.CompanyId).IsRequired().HasColumnName("COMPANY_ID");
ToTable("CMS_USERS_TO_COMPANIES", "GMATEST");
}
}
Here's the error I get:
'((new
System.Linq.SystemCore_EnumerableDebugView(ctx.FindAll())).Items[0]).Companies'
threw an exception of type
'System.Data.EntityCommandExecutionException'
Inner exception: ORA-00942: table or
view does not exist
You do not really need 3 tables to perform the mapping. You could simply do the following:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Company> Companies { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
And this should do it. As long as you have a virtual ICollection referencing the other entity in both POCO's, CTP5's conventions should take care of everything for you. On the other hand, if you prefer doing it manually using the Fluent API, then check this blog by the ADO.NET team (scroll a little down to the many-to-many relationship section)
The answer for my problem, i have to say many thanks to you all, but Andrey from Devart,
have given me the solution, it's simple.
First off all i have to have, three tables Companies, Users, UsersCompanies/
Second off all i have to map the tables to original names of tables example:
my users table called in Db like that: CMS_USERS i have to map it with original name.
this is the example that i use and it's really works.
HasMany(c => c.Companies)
.WithMany(u => u.Users)
.Map(mc =>
{
mc.ToTable("CMS_USERS_TO_COMPANIES", "GMATEST");
mc.MapLeftKey(c => c.UserId, "USER_ID");
mc.MapRightKey(u => u.CompanyId, "COMPANY_ID");
});
Thank you all.

Resources