I have a route on my WebAPI project that accepts an object as input ExportPostData. ExportPostData has a property called "contract" of type Contract which was successfully being populated when I called the route. I added the [OnDeserialized] tag to the Contract class and now it always fails deserialization. There are no errors thrown, just Contract is null. I have no idea how to debug this since my OnDeserialized method never even gets hit.
ExportPostData
public class ExportPostData
{
public Contract contract { get; set; }
public bool includeSubItems { get; set; }
public string user { get; set; }
public string[] projects { get; set; }
}
Contract
public class ZEstimateContract
{
public string _id { get; set; }
public string contractName { get; set; }
public string contractNumber { get; set; }
public string updatedBy { get; set; }
public DateTime updated_at { get; set; }
[OnDeserialized()]
internal void Deserialized()
{
// THIS NEVER GETS HIT
Console.WriteLine("I'm deserialized");
}
}
Change
[OnDeserialized()]
internal void Deserialized()
{
// THIS NEVER GETS HIT
Console.WriteLine("I'm deserialized");
}
to this:
[OnDeserialized]
internal void Deserialized(StreamingContext context)
{
// THIS GETS HIT NOW
Console.WriteLine("I'm deserialized");
}
Without the parameter, the method's signature doesn't match what OnDeserialized is looking for. See this article for details: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx
Related
I am using aspnetboilerplate and added below configuration in preintiliaze in module. I have also added data annotation Audited to my entity but still it is not working. My entity is inheriting from AuditedEntity as don't need deleted feature. Please help
Configuration.EntityHistory.IsEnabled = true; Configuration.EntityHistory.Selectors.Add(new NamedTypeSelector("Abp.AuditedEntities", type => typeof(AuditedEntity).IsAssignableFrom(type)));
I have taken reference from here Can't enable Entity History in ASP.NET Zero
Below is entity definition
[Audited]
public partial class QuestionResponse : AuditedEntity<long>
{
public long ApplicationId { get; set; }
public long QuestionId { get; set; }
public string Response { get; set; }
public string Remark { get; set; }
public bool IsActive { get; set; }
public Application Application { get; set; }
public AbpUsers CreatorUser { get; set; }
public AbpUsers LastModifierUser { get; set; }
public Question Question { get; set; }
}
AuditedEntity<long> is not assignable to AuditedEntity.
Add a selector based on the interface IAuditedEntity instead.
Configuration.EntityHistory.Selectors.Add(
new NamedTypeSelector("Abp.AuditedEntities", type =>
// typeof(AuditedEntity).IsAssignableFrom(type)));
typeof(IAuditedEntity).IsAssignableFrom(type)));
Reference
From aspnetboilerplate/aspnetboilerplate's AuditedEntity.cs:
public abstract class AuditedEntity : AuditedEntity<int>, IEntity
{
}
public abstract class AuditedEntity<TPrimaryKey> : CreationAuditedEntity<TPrimaryKey>, IAudited
{
...
}
I am creating POC using Asp.Net Web API. For mapping one object type to another i am using AutoMapper(v5.1.1). Here are the types which is being used for mapping:
//Entity
public class Goal : IVersionedEntity
{
public virtual int GoalId { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual DateTime EndDate { get; set; }
public virtual string Reward { get; set; }
public virtual DateTime? DisabledDate { get; set; }
public virtual byte[] Version { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Schedule> Schedules { get; set; }
}
//Model
public class Goal
{
private List<Link> _links;
public int GoalId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
//public Status Status { get; set; }
public string Reward { get; set; }
public DateTime? DisabledDate { get; set; }
public User User { get; set; }
public ICollection<Schedule> Schedules { get; set; }
public List<Link> Links
{
get { return _links ?? (_links = new List<Link>()); }
set { _links = value; }
}
public void AddLink(Link link)
{
_links.Add(link);
}
}
I am mapping Goal Entity to Goal model type object as following:
public async System.Threading.Tasks.Task Configure()
{
Mapper.Initialize(cfg => cfg.CreateMap<Data.Entities.Goal, Models.Goal>()
.ForMember(m => m.Links, i => i.Ignore()));
}
and here is the 'AutoMapperConfigurator' class in 'App_Start':
public void Configure(IEnumerable<IAutoMapperTypeConfigurator> autoMapperTypeConfigurations)
{
autoMapperTypeConfigurations.ToList().ForEach(m => m.Configure());
Mapper.AssertConfigurationIsValid();
}
But it is throwing following exception:
The following property on TestApp.Web.Api.Models.Goal cannot be
mapped: Add a custom mapping expression, ignore, add a custom
resolver, or modify the destination type TestApp.Web.Api.Models.Goal.
Context: Mapping from type TestApp.Data.Entities.Goal to
TestApp.Web.Api.Models.Goal Exception of type
'AutoMapper.AutoMapperConfigurationException' was thrown.
See it's not showing which property is not getting mapped.
Any help for this isssue.
After spending hours on this, my final findings are follows:
You must have all your entity models and service models mapping correct to make it work. Even if one fails, the mentioned exception will be thrown. And if your complex type mappings are not correct you will get the above error.
In my case, I was missing how to configure the Complex Type with AutoMapper.
To configure Complex Type, either add .ForMember(m => m.Property, i => i.Ignore()) to ignore the complex type mapping if not needed or .ForMember(m => m.Property, i => i.MapFrom(j => Mapper.Map<Entity,ServiceModel>(j.Property))) for nested mapping (refer: http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/) or use CustomMapping if there is come specific requirement during the mapping
I've noticed (even in Web API 2.1) that deep parameter types get filled (processed by the model binder) only on the first level. That is :
public class Person
{
public string Name { get; set; }
public PersonDetails Details { get; set; }
}
public class PersonDetails
{
public string Address { get; set; }
public int Age { get; set; }
}
// ...
public class PersonController : ApiController
{
[HttpPost]
public void ProcessPerson(Person person)
{
// person.Name is filled in correctly
// person.Details.Address and person.Details.Age are not filled in correctly. That is, they have default values (null and 0)
}
}
Is there a simple solution for this problem, except flatting out the Person class like so ?
public class PersonData
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
Later edit 1 :
If I flatten the Person class I get all the data correctly
The request is made by POST (and not GET) because I need to ensure there is no caching and since the operation alters state it would be semantically incorrect to use GET
I have a handful of email templates and in each template I have a header and footer that all share the same info.
The header and footer are represented by EmailModel.cs
public class EmailModel
{
public string CompanyName { get { return ConfigurationManager.AppSettings["CompanyName"]; } }
public string PhoneNumber { get { return ConfigurationManager.AppSettings["PhoneNumber"]; } }
public string FacebookUrl { get { return ConfigurationManager.AppSettings["FacebookUrl"]; } }
public string TwitterUrl { get { return ConfigurationManager.AppSettings["TwitterUrl"]; } }
public string YouTubeUrl { get { return ConfigurationManager.AppSettings["YouTubeUrl"]; } }
//Additional methods for sending these templates as emails
}
Now for a specific email template I have a view model.NewSignUpEmailViewModel.cs
Should I do this:
public class NewSignUpEmailViewModel : EmailModel
{
public string FirstName { get; set; }
public string CompanyName { get; set; }
public string UserName { get; set; }
public Guid UserId { get; set; }
}
or this:
public class NewSignUpEmailViewModel
{
public EmailModel Email {get; set;}
public string FirstName { get; set; }
public string CompanyName { get; set; }
public string UserName { get; set; }
public Guid UserId { get; set; }
}
I just used email as an example, is there pros/cons to each?
The only con I can see is that in some cases you will run into duplicate property name issue.
Composition is often preferred over inheritance, but both have their place. One good rule of thumb is to determine if there is an "is-a" or a "has-a" relationship between your objects. If object 1 has object 2 as a component, composition is definitely the way to go.
As an example, let's approach your data model a bit differently:
public class SocialLinks
{
public string FacebookUrl { get; set; }
public string TwitterUrl { get; set; }
public string YouTubeUrl { get; set; }
}
public class User
{
public SocialLinks links { get; set; }
public string Name { get; set; }
// and so on
}
In this example, it's obvious that a user HAS social web links, as opposed to the user being a specialized version of the SocialLinks class. Hope that helps!
I have searched hi and low and I am stuck here.
I am using EF 4.1 in an MVC3 app, with the Service/Repository/UnitOfWork pattern and AutoMapper to map my models and entities.
So I have a really basic situation; I have a collection of ChildProducts that have a collection of PriceTiers.
My view models look like this:
AddEditChildProductModel
public class AddEditChildProductModel
{
#region "Fields/Properties"
public ActionType ActionType { get; set; }
public string FormAction { get; set; }
public int ID { get; set; }
public int ProductID { get; set; }
public string Sku { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Cost { get; set; }
public decimal MSRP { get; set; }
public decimal RetailPrice { get; set; }
public int Servings { get; set; }
public decimal Weight { get; set; }
public bool Display { get; set; }
public int DisplayIndex { get; set; }
public IEnumerable<AddEditPriceTierModel> PriceTiers { get; set; }
#endregion
#region "Constructor(s)"
#endregion
#region "Methods"
#endregion
}
AddEditPriceTierModel
public class AddEditPriceTierModel
{
#region "Fields/Properties"
public int ID { get; set; }
public int ChildProductID { get; set; }
public decimal Price { get; set; }
public int QuantityStart { get; set; }
public int QuantityEnd { get; set; }
public bool IsActive { get; set; }
#endregion
#region "Constructor(s)"
#endregion
#region "Methods"
#endregion
}
In the controller action, I am simply trying to map the changed PriceTier properties:
public ActionResult EditChildProduct(AddEditChildProductModel model)
{
if (!ModelState.IsValid)
return PartialView("AddEditChildProduct", model);
ChildProduct childProduct = productService.GetChildProductByID(model.ID);
AutoMapper.Mapper.Map<AddEditChildProductModel, ChildProduct>(model, childProduct);
UnitOfWork.Commit();
return ListChildProducts(model.ProductID);
}
And I am getting this error:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
When stepping into the action, the models/entities are mapped correctly, I don't get it!!
Eranga is right. I'm guessing your productService does not call AsNoTracking on the ef context before returning the ChildProduct. If not, this means what it returns is still attached to the context. When automapper does its thing, it replaces the whole collection, which can orphan the attached child entities that were not part of the form submission. Since the orphans don't have a non-null foreign key, they must be deleted from the context before calling SaveChanges. If they are not, you get this infamous exception.
On the other hand, if your productService calls AsNoTracking on the context before returning the entity, it will not track changes, and will not try to delete any orphaned items that do not exist in the collection created by automapper.