AutoQuery with ResponseDTO in ServiceStack - servicestack

I have created an AutoQuery function in my API with the use of the following code.
[Route("/cars/search")]
public class SearchCars : QueryDb<Car, CarDto>, IJoin<Car, Equipment, Colour, FuelType, Manufacturer>
{
public List<int> EquipmentIds { get; set; }
public List<int> ManufacturerIds { get; set; }
public List<int> ColourIds { get; set; }
}
The CarDto looks like this
public class CarDto
{
public int Id { get; set; }
public List<Equipment> Equipment { get; set; }
public Manufacturer Manufacturer { get; set; }
public int ManufactorId { get; set; }
public FuelType FuelType { get; set; }
public int FuelTypeId { get; set; }
public byte[] ProfileImage { get; set; }
}
I was wondering if there are any specific values the IJoin looks for, because when I try to use this I get "Could not infer relationship between Car and Equipment"
Car looks like this.
[AutoIncrement]
public int Id { get; set; }
public int FuelTypeId { get; set; }
[Required]
public List<Equipment> Equipment { get; set; }
[Required]
public List<int> EquipmentIds { get; set; }
[Required]
public byte[] ProfileImage { get; set; }
Equipment looks like this.
[AutoIncrement]
public int Id { get; set; }
public EquipmentType EquipmentType { get; set; }
[Required]
public int EquipmentTypeId { get; set; }
[Required]
public string Name { get; set; }
I realise that there would require alot of magic knowing that EquipmentIds is a list of Ids that it should check for in the Equipment table, but since everything else with ServiceStack is magic, I am giving it a try.
NB I have shortened some of the models because they are long and contains lots of information which is not needed in this case.

Any joins in AutoQuery need to follow OrmLite's Reference Conventions.

Related

Automapper is mapping incorrectly?

I have two objects, Project is a DB entity and IProject which I am trying to save to the database:
public interface IProject
{
int ProjectID { get; set; }
string JobNumber { get; set; }
string JobName { get; set; }
string ProjectLocation { get; set; }
int? FeeTypeID { get; set; }
decimal? Fee { get; set; }
decimal? ConstructionCost { get; set; }
decimal? CostPerSquareFoot { get; set; }
decimal? SquareFoot { get; set; }
string Memo { get; set; }
DateTime? DateCompleted { get; set; }
int JobStatusID { get; set; }
int ProjectTypeID { get; set; }
}
//The inheritance is actually in a partial class that doesn't get overridden
public partial class Project : IProject
{
public Project()
{
this.ProjectContacts = new HashSet<ProjectContact>();
this.ProjectConsultants = new HashSet<ProjectConsultant>();
}
public int ProjectID { get; set; }
public string JobNumber { get; set; }
public string JobName { get; set; }
public Nullable<int> FeeTypeID { get; set; }
public Nullable<decimal> Fee { get; set; }
public int JobStatusID { get; set; }
public string Memo { get; set; }
public Nullable<decimal> Acreage { get; set; }
public Nullable<decimal> SquareFoot { get; set; }
public Nullable<decimal> ConstructionCost { get; set; }
public Nullable<decimal> BudgetPrice { get; set; }
public Nullable<decimal> ActualPrice { get; set; }
public Nullable<System.DateTime> DateCompleted { get; set; }
public Nullable<decimal> CostPerSquareFoot { get; set; }
public string ProjectLocation { get; set; }
public int ProjectTypeID { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<int> PhaseID { get; set; }
public string ProjectDescription { get; set; }
public Nullable<int> ArchitectID { get; set; }
public Nullable<int> ManagerID { get; set; }
public Nullable<int> ArchiveLocationID { get; set; }
public virtual ICollection<ProjectContact> ProjectContacts { get; set; }
public virtual FeeType FeeType { get; set; }
public virtual JobStatu JobStatu { get; set; }
public virtual ICollection<ProjectConsultant> ProjectConsultants { get; set; }
public virtual Person Person { get; set; }
public virtual ArchiveLocation ArchiveLocation { get; set; }
public virtual Person Person1 { get; set; }
public virtual Phase Phase { get; set; }
public virtual ProjectType ProjectType { get; set; }
}
I have my Map created:
Mapper.CreateMap<IProject, Data.Project>();
but for some reason whenever I call the map:
//mappingService is a wrapper I have around Automapper so that I can inject it
var project = _mappingService.Map<IProject, Data.Project>(request);
I get this error:
Missing type map configuration or unsupported mapping.
Mapping types:
RuntimeType -> FeeType
System.RuntimeType -> Renaissance.Data.FeeType
Destination path:
Project.FeeType.FeeType
Source value:
System.Decimal
I am not at any point trying to do that. The names match and even if I specifically tell it to map with:
.ForMember(dest => dest.Fee, opt => opt.MapFrom(src => src.Fee))
It still errors with the same message.
What am I missing?
UPDATE
This works:
.ForMember(dest => dest.FeeType, opt => opt.Ignore());
I think it is a hack that I need to do that. I shouldn't have to ignore a property in the destination that does not exist / is not named the same as something in the source property.

CRUD multiple model in one view in mvc4

i want use multiple model in one view and add records in multiple table from view
my db like below image:
(i work with vs2012 mvc4 and EF)
i create four model for each table and class "PreOrder" for repository of all
public class Orders
{
public long OrdersId { get; set; }
public string CustomerId { get; set; }
public long OrderListId { get; set; }
public int? CountProduct { get; set; }
public string CountPrice { get; set; }
public string VisitorsName { get; set; }
public DateTime? OrderDate { get; set; }
}
public class Product
{
public string ProductID { get; set; }
public string NameProduct { get; set; }
public string Price { get; set; }
}
public class Customers
{
public string CustomerID { get; set; }
[DisplayFormat(NullDisplayText = "-")]
public string Name { get; set; }
[DisplayFormat(NullDisplayText = "-")]
...
}
public class OrderList
{
public long OrderListID { get; set; }
public Nullable<long> OrdersId { get; set; }
public string ProductId { get; set; }
public Nullable<int> Count { get; set; }
public Nullable<decimal> DisCount { get; set; }
}
public class PreOrder
{
public Customers _Customer { set; get; }
public Product _Product { set; get; }
public Orders _Order { set; get; }
public OrderList _OrderList { set; get; }
}
i want use name,family,customerid from tblcustomers
and productId,NameProduct,Price from tblProducts
and all fields of tblOrders and tblOrderList
how can i create one view to fill tables Orders and OrderList??
i solved problem like below article:
How to Use ViewModel with ASP.NET MVC ?
How to Use ValueInjecter with Asp.net MVC ViewModel ?

EF Code First - Many To Many

I have a problem with devising a many to many relationship in code first. EF is creating the Junction table and associating the Fk's as I would expect, however when i try to access the User's MailingList collection, there are no entries.
I've implemented test data on Initialise via Seeding, the data is al present in the database.
I think the problem lies with the constructors for Users and MailingLists, but I'm uncertain. I want to be able to navigate the navigational property of User.MailingLists.
var user = db.Users.Find(1);
Console.WriteLine("{0}", user.EmailAddress); //This is Fine
Console.WriteLine("{0}", user.Address.PostCode); /This is Fine
foreach (MailingList ml in user.MailingLists) // this is 0
{
Console.WriteLine("{0}", ml.Name);
}
My model is below:-
public class User : IEntityBase
{
public User()
{
MailingLists = new List<MailingList>();
}
public int Id { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
public virtual Address Address { get; set; }
public ICollection<MailingList> MailingLists { get; set; }
}
public class MailingList : IEntityBase
{
public MailingList()
{
Users = new List<User>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
public ICollection<User> Users { get; set; }
}
public class Address : IEntityBase
{
public int Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
public DateTime? DateLastUpdated { get; set; }
public DateTime DateCreated { get; set; }
public bool IsDeleted { get; set; }
}
Any suggestions welcome.
You are neither eager loading the MailingList entries with the query, nor fulfulling the requirements for a lazy loading proxy so there is no way EF can populate the collection.
To allow lazy loading, change the MailingList property to be virtual to allow the EF proxy to override it.
To use eager loading, use Include() (an extension method in System.Data.Entity) in the query to specify that the MailingList should be loaded.

automapper Missing type map configuration or unsupported mapping

I'm trying to map the following entities:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
public DateTime DOB { get; set; }
public List<Telephone> Telephones { get; set; }
}
public class SearchPersonViewModel
{
public int Id { get; set; }
public string FullName { get; set; }
public string LicencePlate { get; set; }
public string CarMake { get; set; }
public string CarModel { get; set; }
}
I do that:
AutoMapper.Mapper.CreateMap<List<SearchPersonViewModel>, List<Person>>();
List<SearchPersonViewModel> model = AutoMapper.Mapper.Map<List<Person>, List<SearchPersonViewModel>>(persons);
And I get the exception 'Missing type map configuration or unsupported mapping.'
I don't see what's wrong with my code, anyone could help ?

Mapping from list down to object with AutoMapper

I'm new with AutoMapper and have a problem I'm trying to solve.
If I have a source class like this:
public class Membership
{
public int MembershipId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string OrganizationName { get; set; }
public List<Address> Addresses { get; set; }
}
And the Address class looks like this:
public class Address
{
public int AddressId{ get; set; }
public int RefAddressTypeId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public bool IsPreferredAddress { get; set; }
}
My destination class is:
public class UserInformationModel
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Organization { get; set; }
public string EmailAddress { get; set; }
public PhysicalAddress BillingAddress { get; set; }
public PhysicalAddress ShippingAddress { get; set; }
}
And the destination address class is:
public class PhysicalAddress
{
public AddressType AddressType{get; set;}
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
I have set up a mapping like this:
Mapper.CreateMap<MinistryMattersIntegration.BusinessObjects.Entities.Cokesbury.Membership, UserInformationModel>()
.ForMember(dest => dest.Organization, opt => opt.MapFrom(src=>src.OrganizationName));
This is working for Membership to UserInformationModel, but now I need to get addresses working. One important thing to note, though, is that the destination is a single billing address and a single shipping address while in the original model, all the address are stored as a list. The way you find the shipping and billing addresses out of the list is by looking at the RefAddressTypdId and the IsPreferredAddress. Only one preferred address may exist with a particular RefAddressTypeId.
So, my question is, how do you get AutoMapper to do this kind of mapping? Is it possible, or am I better off just going with regular mapping code?
You'll want to use the Custom Value Resolvers feature of AutoMapper. So you'd setup a Custom Resolver to map from your list to your single entity using the IsPreferredAddress flag to find it.
The documentation is pretty good for the Custom Resolvers so you should be fine figuring it out from there.

Resources