i am currently using a .NET Web Api, to get a list of Counties based on Countries.
While attempting to GET the Counties, I get the "Automapper: Missing type map configuration or unsupported mapping" exception.
My Domain Class is :
public class County
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual IList<City> City { get; set; }
}
My DTO is
public class CountyResponse
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
}
Mapping profile :
public class CountyProfile : Profile
{
public CountyProfile()
{
CreateMap<CountyResponse, County>().ReverseMap();
}
}
My Service Class GetAlLMethod:
public async Task<Result<List<CountyResponse>>> GetAllAsync()
{
var counties = _context.Counties.ToListAsync();
var mappedCounties = _mapper.Map<List<CountyResponse>>(counties);
return await Result<List<CountyResponse>>.SuccessAsync(mappedCounties);
}
Builds Ok, and I also have the Country domain mapped exactly the same (1:1).
Yet, one works, while this one, when trying to access the Get Endpoint, i get this error.
Any idea what is wrong here ? (the services are registered - I am using the following to inject them :
public static void AddInfrastructureMappings(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
}
You are mapping a task (the result of your ToListAsync) to a list of your DTO which fails. You need to await your query so you get the result of the task, then the mapping will work properly
var counties = await _context.Counties.ToListAsync();
var mappedCounties = _mapper.Map<List<CountyResponse>>(counties);
What is a good way is to automap an object with some properties, one of which is a JSON string, to another object where that JSON string would be represented by properties instead of JSON.
See the source and destination classes below for an example:
CustomJson will look something like this for this example but could be different for other AlertTypes: { "AlertPrice": 500.0 }
//This is the database table representation (source)
public class Alert
{
public int Id { get; set; }
public int UserId { get; set; }
public string AlertType { get; set; }
public string CustomJson { get; set; }
}
//This is the business logic model (destination)
public class UserAlert
{
public int Id { get; set; }
public int UserId { get; set; }
public string AlertType { get; set; }
//This comes from the CustomJson string in the DB
//A different AlertType may need to map to a different object with different properties
public decimal AlertPrice { get; set; }
}
Will a ValueResolver allow me to do this or will it only be able to map part of the object? If not, perhaps I should just have a Custom property which contain the custom info instead of trying to meld it into the top-level object.
CreateMap<sourceobject, destobject>()
.AfterMap((src, dest) =>
{
dest.AlertPrice = JsonConvert.DeserializeObject<T>(json).AlertPrice;
});
CreateMap<sourceobject, destobject().AfterMap((src, dest) =>
{
dest.AlertPrice = decimal.Parse(src.AlertPrice, CultureInfo.InvariantCulture);
});
Please use above code you have use Explicity Mapping
I am using auto mapper and able to map my data objects to business objects with out any issue. But when i try to map data object that is having virtual property, it is throwing error:
AutoMapper.AutoMapperMappingException:'Unable to create a map expression from AdmissionStatu.AdmissionStatusLookup to AdmissionStatus.AdmissionStatusLookup'
My business object is:
public class AdmissionStatus
{
public long AccountID { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
public AdmissionStatusLookup AdmissionStatusLookup { get; set;}
}
My data object is:
public partial class AdmissionStatu
{
public long AccountID { get; set; }
public string Description { get; set; }
public string Comment { get; set; }
public virtual AdmissionStatusLookup AdmissionStatusLookup { get; set; }
}
My query in repository is:
var query = from rows in dataContext.AdmissionStatus
where rows.AdmissionID == admissionId
select rows;
return ToBusinessObjects(query);
This is auto mapper mapping configuration:
protected virtual IConfigurationProvider CreateAutomappingConfiguration()
{
return new MapperConfiguration(c =>
{
c.CreateMap<DataType, BusinessType>();
c.CreateMap<BusinessType, DataType>();
});
}}
and ToBusinessObjects method is:
protected List<BusinessType> ToBusinessObjects(IQueryable<DataType> query)
{
return query.ProjectTo<BusinessType>(mappingConfig).ToList();
}
When ever i call query, i am getting error in ToBusinessObjects method. I am using same set up for mapping all data objects to business objects and vice-versa in the same way and it is working fine. But when i am mapping objects having virtual property it is not working. Could some one help me what went wrong here.
Try this
return Mapper.Map<List<BusinessType>>(query.ProjectTo<BusinessType>(mappingConfig).ToList())
I am using an Automapper to map one model to second model which has some rows with the same name as the first model. I am getting this inner exception
Missing type map configuration or unsupported mapping.
Mapping types:
Bed -> Bed
Model1.Bed -> Model2.Bed
This is the condensed version of the code.
Model 1
public class Model1
{
public Guid Id { get; set; }
public string Name { get; set; }
public IEnumerable<Bed> Beds { get; set; }
public IEnumerable<Bed> Beds1 { get; set; }
public string Status {get; set;}
public string Notes {get; set;}
}
public class Model2
{
public Guid Id { get; set; }
public string Name { get; set; }
public IEnumerable<Bed> Beds { get; set; }
public IEnumerable<Bed> Beds1 { get; set; }
}
public class Bed
{
public Guid Id { get; set; }
public Guid TypeId { get; set; }
public string Type { get; set; }
public string Notes { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? LastUpdatedOn { get; set; }
}
Mapping :
MapperConfiguration config = null;
config = new MapperConfiguration(cfg => { cfg.CreateMap<Model1, Model2>(); });
IMapper mapper = config.CreateMapper();
var dest = mapper.Map<Model1, Model2>(update);
update is an instance of Model1. Am I missing something? Should I do something with regards to the Bed class mapping even though both reference the same method?
Also is there a way to copy data from one model to another in this scenario other than manually doing it or using Automapper?
EDIT 1:
I am sure the error is from that mapping. I removed it and all the other fields were fine and there was no exception. But I am not sure how I should do the Bed -> Bed binding.
EDIT 2:This link seems to have the same issue and they say it's a possible bug and This one seems to have the answer although I am not sure how I can adapt it to my situation.
It seems like auto mapper is having trouble determining how to map Bed. I would try the following:
config = new MapperConfiguration(cfg => {
cfg.CreateMap<Model1, Model2>();
cfg.CreateMap<Bed, Bed>();
});
Although it seems odd that would be necessary...
I am getting an a Circular Reference Serialization Error although, to my knowledge I do not have any circular references. I am retrieving a set of Orders from the database and sending them to the client as JSON. All the code is shown below.
This is the error:
Error
A circular reference was detected
while serializing an object of type
'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.
Exception Details:
System.InvalidOperationException: A
circular reference was detected while
serializing an object of type
'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
Source Error:
An unhandled exception was generated
during the execution of the current
web request. Information regarding the
origin and location of the exception
can be identified using the exception
stack trace below.
My classes are as follows:
Order
public class Order
{
[Key]
public int OrderId { get; set; }
public int PatientId { get; set; }
public virtual Patient Patient { get; set; }
public int CertificationPeriodId { get; set; }
public virtual CertificationPeriod CertificationPeriod { get; set; }
public int AgencyId { get; set; }
public virtual Agency Agency { get; set; }
public int PrimaryDiagnosisId { get; set; }
public virtual Diagnosis PrimaryDiagnosis { get; set; }
public int ApprovalStatusId { get; set; }
public virtual OrderApprovalStatus ApprovalStatus { get; set; }
public int ApproverId { get; set; }
public virtual User Approver { get; set; }
public int SubmitterId { get; set; }
public virtual User Submitter { get; set; }
public DateTime ApprovalDate { get; set; }
public DateTime SubmittedDate { get; set; }
public Boolean IsDeprecated { get; set; }
}
Patient
public class Patient
{
[Key]
public int PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleInitial { get; set; }
public bool IsMale;
public DateTime DateOfBirth { get; set; }
public int PatientAddressId { get; set; }
public Address PatientAddress { get; set; }
public bool IsDeprecated { get; set; }
}
Certification Period
public class CertificationPeriod
{
[Key]
public int CertificationPeriodId { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public bool isDeprecated { get; set; }
}
Agency
public class Agency
{
[Key]
public int AgencyId { get; set; }
public string Name { get; set; }
public int PatientAddressId { get; set; }
public virtual Address Address { get; set; }
}
Diagnosis
public class Diagnosis
{
[Key]
public int DiagnosisId { get; set; }
public string Icd9Code { get; set; }
public string Description { get; set; }
public DateTime DateOfDiagnosis { get; set; }
public string Onset { get; set; }
public string Details { get; set; }
}
OrderApprovalStatus
public class OrderApprovalStatus
{
[Key]
public int OrderApprovalStatusId { get; set; }
public string Status { get; set; }
}
User
public class User
{
[Key]
public int UserId { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NPI { get; set; }
public string Email { get; set; }
}
NOTE: ADDRESS CLASS IS NEW ADDITION DURING EDIT
Address
public class Address
{
[Key]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Title { get; set; }
public string Label { get; set; }
}
The code that executes the serialization is here:
Excerpt from OrderController
public ActionResult GetAll()
{
return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet);
}
Thanks
You could try to remove the virtual keyword from all navigation properties to disable lazy loading and proxy creation and then use eager loading instead to load the required object graph explicitely:
public ActionResult GetAll()
{
return Json(ppEFContext.Orders
.Include(o => o.Patient)
.Include(o => o.Patient.PatientAddress)
.Include(o => o.CertificationPeriod)
.Include(o => o.Agency)
.Include(o => o.Agency.Address)
.Include(o => o.PrimaryDiagnosis)
.Include(o => o.ApprovalStatus)
.Include(o => o.Approver)
.Include(o => o.Submitter),
JsonRequestBehavior.AllowGet);
}
Referring to your previous post it looks like your application isn't relying on lazy loading anyway because you introduced there the virtual properties to load the object graph lazily, possibly causing now the serialization trouble.
Edit
It's not necessary to remove the virtual keyword from the navigation properties (which would make lazy loading completely impossible for the model). It's enough to disable proxy creation (which disables lazy loading as well) for the specific circumstances where proxies are disturbing, like serialization:
ppEFContext.Configuration.ProxyCreationEnabled = false;
This disables proxy creation only for the specific context instance ppEFContext.
(I've just seen, #WillC already mentioned it here. Upvote for this edit please to his answer.)
When you know that you need to serialize from a particular context, you can disable proxy creation for that particular query like below. This has worked for me and is better than revising my model classes.
using (var context = new MeContext())
{
context.Configuration.ProxyCreationEnabled = false;
return context.cars.Where(w => w.Brand == "Ferrari")
}
This approach takes away the proxy object type for this particular instance of the context so the returned objects are the actual class and therefore serialization is not a problem.
ie:
{Models.car}
instead of
{System.Data.Entity.DynamicProxies.car_231710A36F27E54BC6CE99BB50E0FE3B6BD4462ECA19695CD1BABB79605296EB}
The problem is that your are actually serializing an entity framework generated proxy object. Unfortunatly this has some issues when used with the JSON serializer. You might consider to map your entities to special simple POCO classes for the sake of JSON compatibility.
There is an attribute to add to Entity Framework objects
[ScriptIgnore]
This makes the code not perform Circular references.
I think they have fixed this in the latest version.
Check out the help docs under the section "Serializing and Deserializing JSON -> Serialization and Preserving Object References".
Set this setting when initializing the JSON.Net Serializer:
PreserveReferencesHandling = PreserveReferencesHandling.Objects;
So an example would be this:
var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
string json = JsonConvert.SerializeObject(people, Formatting.Indented, serializerSettings);
I verified that this works with my code first solution, and a circular reference in the navigation properties. If you look at the resulting JSON it should have "$id" and "$ref" properties everywhere.
An alternative solution would be to use anonymous types as the result of a LINQ query.
In my project, I am using lazy loading extensively, and disabling it was not the right thing to do.
An alternative solution, if only some values from objects are necessary, is build an anonymous class and return it, like the example below:
public JsonResult AjaxFindByName(string term)
{
var customers = context.Customers
.Where(c => c.Name.ToUpper().Contains(term.ToUpper())).Take(10)
.AsEnumerable()
.Select(c => new {
value = c.Name,
SSN = String.Format(#"{0:000\-00\-0000}", c.SSN),
CustomerID = c.CustomerID });
return Json(customers, JsonRequestBehavior.AllowGet);
}
The circular reference happens because you use eager loading on the object.
You have a couple of methods:
Turn off eager loading when your loading your Query (linq or lambda)
DbContext.Configuration.ProxyCreationEnabled = false;
Remove the virtual keyword from the Domainmodel
Detach the objects (= no eager loading functionality & no proxy)
Repository.Detach(entityObject)
DbContext.Entry(entityObject).EntityState = EntityState.Detached
Clone the properties
You could use something like AutoMapper to clone the object, don't use the ICloneable interface, because it also clones the ProxyProperties in the object, so that won't work.
In case you are building an API, try using a separte project with a different configuration (that doesn't return proxies)
PS. Proxies is the object that's created by EF when you load it from the Entity Framework. In short: It means that it holds the original values and updated values so they can be updated later. It handles other things to ;-)
For those using the proxy EF/Linq2SQL classes my solution was to simply remove the parent reference on my child entities.
So in my model, I selected the relationship and changed the Parent reference to be Internal rather than Public.
May not be an ideal solution for all, but worked for me.
You can remove the virtual keyword:
public virtual Patient Patient { get; set; } -> public Patient Patient { get; set; }
Keep in mind that when you remove the virtual keyword, lazy loading will be turned off.
I was able to solve this problem by using the method described here:
http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/