WebAPI AND Odata V4 issue: ComplexType containing Entities - asp.net-web-api

I am getting following error while adding relationship inside the complex type. How can I fix this issue. I binged and read that it was issue with OData V3 but not in OData V4.
The complex type 'Microsoft.OneIM.ActiveIncident.Contracts.IncidentImpact' refers to the entity type 'Microsoft.OneIM.ActiveIncident.Contracts.ImpactedService' through the property 'ImpactedServices'.
at System.Web.OData.Builder.ODataConventionModelBuilder.MapComplexType(ComplexTypeConfiguration complexType)
at System.Web.OData.Builder.ODataConventionModelBuilder.MapType(StructuralTypeConfiguration edmType)
at System.Web.OData.Builder.ODataConventionModelBuilder.AddComplexType(Type type)
at System.Web.OData.Builder.ODataConventionModelBuilder.ReconfigureEntityTypesAsComplexType(EntityTypeConfiguration[] misconfiguredEntityTypes)
at System.Web.OData.Builder.ODataConventionModelBuilder.RediscoverComplexTypes()
at System.Web.OData.Builder.ODataConventionModelBuilder.GetEdmModel()
at Microsoft.OneIM.ActiveIncident.Service.ModelBuilder.BuildIncidentModels() in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\Service\Models\ModelBuilder.cs:line 42
at Microsoft.OneIM.ActiveIncident.Service.WebApiConfig.Register(HttpConfiguration config) in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\Service\App_Start\WebApiConfig.cs:line 22
at Microsoft.OneIM.ActiveIncident.ServiceHost.ApiStartup.Configuration(IAppBuilder appBuilder) in c:\OneIM\EngSys\OneIM\ActiveIncident\src\Product\ServiceHost\ApiStartup.cs:line 27
My model looks as below
public class Incident
{
public IncidentImpact Impact { get; set; }
}
[ComplexType]
public class IncidentImpact
{
public bool IsCustomerImpacting { get; set; }
public string SupportTicketId { get; set; }
public ICollection<ImpactedService> ImpactedServices { get; set; }
}
public class ImpactedService
{
public long Id { get; set; }
public long IncidentId { get; set; }
public Incident Incident { get; set; }
public long ServiceId { get; set; }
}

Although OData protocol V4 support complex type contains entity as navigation property, both of OData Lib and WebAPI OData do not implementation this feature now.

You have to set a key property by [key] attribute or in the model builder as
builder.EntitySet<Type>("Types").EntityType.HasKey(t => t.KeyProperty);
Hope it helps.

Related

Map two identical models, nested with automapper

I did some research but I couldn't find exactly what I wanted.
I have an endless menu. I have a MenuDTO and a MenuViewModel that I use for this menu. I had no problem matching between model and DTO, but am having trouble mapping DTO to ViewModel. Obviously I couldn't find the solution, can you help?
My MenuDTO Object
public class MenuDto : BaseDto
{
public string Name { get; set; }
public string Icon { get; set; }
public string Order { get; set; }
public string Url { get; set; }
public bool IsVisible { get; set; }
public int ParentId { get; set; }
public MenuDto ParentMenu { get; set; }
public List<MenuDto> Menus { get; set; }
}
And MenuViewModel
public class MenuViewModel
{
public int Id { get; set; }
public bool IsActive { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public string Order { get; set; }
public string Url { get; set; }
public bool IsVisible { get; set; }
public int ParentId { get; set; }
public MenuViewModel ParentMenu { get; set; }
public List<MenuViewModel> Menus { get; set; }
}
This is how I mapped the MenuDTO and MenuViewModel objects.
public class WebProfile : Profile
{
public WebProfile()
{
CreateMap<MenuDto, MenuViewModel>();
CreateMap<MenuViewModel, MenuDto>();
}
}
I call this way in the controller
var navMenuItems = _mapper.Map<List<MenuViewModel>(_menuService.GetNavMenus());
Although all fields are mapped, I get an error on the Menus field.
The error message I get is;
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
MenuDto -> MenuViewModel
BiPortal2020.Business.ServiceDTOs.Menu.MenuDto -> BiPortal2020.WebUI.Areas.Admin.Models.Menu.MenuViewModel
lambda_method(Closure , MenuDto , MenuViewModel , ResolutionContext )
AutoMapperMappingException: Error mapping types.
Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1
The error message implies - AutoMapper, either cannot map between MenuDto and MenuViewModel, or it cannot locate the defined mappings.
I've tested your mappings and they are totally fine. So, what possibility remains is AutoMapper cannot locate your mappings.
I'm Assuming the Business Layer and UI Layer you mentioned in the comment section are two separate projects. Since the WebProfile is defined in the UI Layer, you have to tell AutoMapper that it should search that assembly to find the mappings. Since your mappings between Models and DTOs are working, I can guess you've already done the same for BusinessProfile which is defined in the Business Layer.
I don't know about your existing code, but you could do something like this - in the Startup.Configure method add/modify the following line -
services.AddAutoMapper(typeof(IDtoMapping), typeof(IViewModelMapping));
where IDtoMapping and IViewModelMapping are two marker interface (empty interface, used only to identify the assembly they are declared in) declared in the Business Layer and UI Layer, respectively.

Entity History is not working in aspnetboilerplate

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
{
...
}

Automapper Many to Many Mapping Including Joining Table Data

I am developing an ASP.Net Core Web API using EF Core Code First (C#) and SQL Server.
I have a fairly simple scenario which I just cannot figure out. I have a Form entity and a Site entity. Each Form can have many Sites and each Site can be in many Forms. To enable this I have a SiteForm joining table. For each Site associated with a Form there is a Leaving Date field. So my SiteForm class looks like this:
public class SiteForm
{
public Guid SiteId { get; set; }
public Site Site{ get; set; }
public Guid FormId { get; set; }
public Form Form{ get; set; }
public DateTime? LeavingDate { get; set; }
}
My Form's Data Transfer Object (FormDto) is as follows:
public class FormDto
{
public Guid Id { get; set; }
...
public ICollection<LinkedSiteDto> LinkedSites { get; set; }
= new List<LinkedSiteDto>();
}
And my LinkedSiteDto is like this:
public class LinkedSiteDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Ident { get; set; }
public DateTime? LeavingDate { get; set; }
}
Having populated the database I can get the Sites for each Form using the following mapping:
CreateMap<Form, FormDto>()
.ForMember(dto => dto.LinkedSites, opt => opt.MapFrom(
form => form.SiteForms.Select(sf => sf.Site).ToList()));
I just cannot figure out how I would include the LeavingDate value from each joining table entry? Any suggestions would be very gratefully received.

Entity Framework POCO Relationships

I am trying to implement code-first approach of entity framework. I have four entities UserInfo, Client, Admin and Account. I want relationships as:
Each Client has a UserInfo
Each Admin has a `UserInfo
Each Account is linked with a User(UserInfo)
Assuming these things i wrote the POCO models. With the relationships i want, is it correct ?Am i missing something?
public class UserInfo
{
public int UserInfoID { get; set; }
public Name Name { get; set; }
public Address Address { get; set; }
public Contact Contact { get; set; }
}
public class Admin
{
public int AdminID { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
public class Client
{
public int ClientID { get; set; }
public CompanyDetails CompanyDetails { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
public class Account
{
public int AccountID { get; set; }
[Required, Column("Balance"), Display(Name = "Account Balance")]
public double Balance { get; set; }
public int UserInfoID { get; set; }
[ForeignKey("UserInfoID")]
public virtual UserInfo UserInfo { get; set; }
}
What you have appears to be correct based on your requirements however I personally prefer the Entity Framework Model Builder when configuring your entities with Code First.
Using the model builder means that you don't have any attributes on your POCO entities which in turn means that you don't need an EF reference to use the entities.
Take a look at my article here for some more info on how to use the modelbuilder : http://blog.staticvoid.co.nz/2012/07/entity-framework-navigation-property.html

What is the proper sequence of method calls when using a multi layered architecture?

I have built a simple survey-tool using MVC 3 with only 1 layer (MVC). I regret this now. All my database access and mapping is handled in the controllers, and some other mapping classes.
I would like to switch over to using three layers:
Presentation (MVC)
Business Logic
Data / Persistence (EF)
I am using the Entity Framework to handle everything with the database. The entity framework creates it's own domain classes. Where should the mapping between the Models that MVC uses and the models that EF creates go?
If the mapping is in the business layer, is there a need for the Models folder in the MVC project?
A survey-question consists of the Question itself, Rows and Columns. Theese are the models that i use:
public class Question {
public int Question_ID { get; set; }
public Boolean Condition_Fullfilled;
[Required(ErrorMessage = "Dette felt er påkrævet")]
public String Question_Wording { get; set; }
public String Question_Type { get; set; }
[Required(ErrorMessage = "Dette felt er påkrævet")]
public String Question_Order { get; set; }
public String Left_scale { get; set; }
public String Right_scale { get; set; }
public int Scale_Length { get; set; }
public String Left_Scale_HelpText { get; set; }
public String Right_Scale_HelpText { get; set; }
public Boolean Visible { get; set; }
public Boolean IsAnswered { get; set; }
public String Question_HelpText { get; set; }
public int Category_ID { get; set; }
}
public class MatrixColumns
{
public int Column_ID { get; set; }
public int Column_Number { get; set; }
public String Column_Description { get; set; }
public Boolean IsAnswer { get; set; }
public int? Procent { get; set; }
public bool Delete { get; set; }
public bool Visible { get; set; }
public int? Numbers { get; set; }
public String Help_Text { get; set; }
}
public class MatrixRows
{
public bool Delete { get; set; }
public bool Visible { get; set; }
public int Row_Id { get; set; }
public String Row_Number { get; set; }
public String Row_Description { get; set; }
public String Special_Row_CSS { get; set; }
public String Help_Text { get; set; }
// Dette er summen af procenterne af alle kolonner i rækken
public int RowSum { get; set; }
}
All the data for theese models is retrieved in the Controller, based upon a QuestionID, and mapped to a ViewModel that looks like this:
public class ShowMatrixQuestionViewModel : Question
{
public Dictionary<MatrixRows, List<MatrixColumns>> columnrow { get; set; }
public List<MatrixColumns> columns { get; set; }
public List<MatrixRows> rows { get; set; }
public ShowMatrixQuestionViewModel()
{
columns = new List<MatrixColumns>();
rows = new List<MatrixRows>();
columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}
So when i want to send a ShowMatrixQuestionViewModel to a View from my Controller, what is the route i should take?
This is my suggestion:
-> Controller calls a method in the business layer called
public ShowMatrixViewModel GetQuestion(int QuestionID) {}
-> GetQuestion calls the following methods in the data layer:
public Question GetQuestion(int QuestionId) {}
public MatrixRows GetRows(int QuestionId) {}
public MatrixColumns GetColumns(int id) {}
-> Entity framework returns "pure" objects, which i want to map over to the ones i posted above
-> GetQuestion calls methods to map the EF models to my own models
-> Last GetQuestion calls a method that maps the Questions, Rows and Columns:
ShowMatrixQuestionViewModel model = MapShowMatrixQuestionViewModel(Question, MatrixRows, MatrixColumns)
return model;
Is this correct?
Thanks in advance
To answer the first part of your question:
"Where should the mapping between the Models that MVC uses and the models that EF creates go?"
The answer is that the models MVC uses are the models created by the EF. Your EF tool in the ASP.NET MVC project is either Linq to SQL Classes or the ADO.NET Entity Framework Model. You should create these inside the Models folder in your project and they provide your data / persistence (EF).

Resources