Is it possible to add array of data in child entity? - c#

I have issue that I hope someone here will be able to help me.For this exercise I`m using aspnetboilerplate framework.
So I create 3 entity and they ALL work good (at least I guess they work good)
Little bit recap of project
One Recepie HAVE one or more Ingredient
Ingrident.cs
public class Ingrident : Entity
{
public string Name { get; set; }
public Master Master { get; set; }
public int? MasterId { get; set; }
public Recepie Recepie { get; set; }
public int? RecepieId { get; set; }
}
Master.cs
public class Master : Entity
{
public string Name { get; set; }
public string? Image { get; set; }
public string? ShortDescription { get; set; }
public string? FullDescription { get; set; }
public string? Keywords { get; set; }
public string Slug { get; set; }
public int? Count { get; set; }
public List<Ingrident> Ingridents { get; set; }
}
Recepie.cs
public class Recepie : Entity
{
public string RecepieName { get; set; }
public Ingrident Ingridents { get; set; }
}
With this database structure I can add Recepie and add only one ingredient when I try to send [] of Ingredient its show me DTO error.
And here is RecepieAppService.cs
public class RecepieAppService : AsyncCrudAppService<IndexIngrident.Entities.Recepie,RecepieDto>
{
private readonly IRepository<IndexIngrident.Entities.Recepie> _repository;
private readonly IRepository<IndexIngrident.Entities.Ingrident> _ingRepository;
public RecepieAppService(IRepository<IndexIngrident.Entities.Recepie> repository, IRepository<IndexIngrident.Entities.Ingrident> ingRepository)
: base(repository)
{
_repository = repository;
_ingRepository = ingRepository;
}
public List<RecepieFullGetDto> GetAllIncluded()
{
var result = _repository.GetAllIncluding(x => x.Ingridents , x => x.Ingridents.Master);
Debug.WriteLine(result);
return ObjectMapper.Map<List<RecepieFullGetDto>>(result);
}
}
RecepieDto.cs
[AutoMap(typeof(IndexIngrident.Entities.Recepie))]
public class RecepieDto : EntityDto
{
public string RecepieName { get; set; }
public IngridentRecepieDto Ingridents { get; set; }
}
public class IngridentRecepieDto : EntityDto
{
public string Name { get; set; }
public int RecepieId { get; set; }
}
Using AsyncCrudAppService my CRUD is automatically generated and I`m able to create new Recepie but when I try to do something like this
I get error

you need to add the mapping of objects in both directions, [AutoMapTo (typeof (RecepieDto))] and [AutoMapFrom (typeof (Recepie))], and verify that you have the class, inherits AutomapperProfile

Related

Auto mapper makes error when mapping class with another class properties

I have following two class structures, its CASADetialsResponse as follows,
public class CASADetialsResponse
{
public string status { get; set; }
public string message { get; set; }
public List<object> validation { get; set; }
public Data data { get; set; }
}
public class LinkedCard
{
public string cardNumber { get; set; }
}
public class JointParty
{
public string jointName { get; set; }
}
public class Account
{
public string accountNumber { get; set; }
public string accountNickName { get; set; }
public List<LinkedCard> linkedCards { get; set; }
public List<JointParty> jointParty { get; set; }
public List<object> productValidation { get; set; }
}
public class Transaction
{
public string accountNumber { get; set; }
public string valueDate { get; set; }
public string transactionDetails { get; set; }
public string postDate { get; set; }
}
public class Data
{
public Account account { get; set; }
public List<Transaction> transactions { get; set; }
}
And the CASADetails class structure as follows,
public class CASADetails
{
public string status { get; set; }
public string message { get; set; }
public List<object> validation { get; set; }
public Data data { get; set; }
}
public class LinkedCard
{
public string cardNumber { get; set; }
}
public class JointParty
{
public string jointName { get; set; }
}
public class Account
{
public string accountNumber { get; set; }
public string accountNickName { get; set; }
public List<LinkedCard> linkedCards { get; set; }
public List<JointParty> jointParty { get; set; }
public List<object> productValidation { get; set; }
}
public class Transaction
{
public string accountNumber { get; set; }
public string valueDate { get; set; }
public string transactionDetails { get; set; }
public string postDate { get; set; }
}
public class Data
{
public Account account { get; set; }
public List<Transaction> transactions { get; set; }
}
When I use _mapper.Map<CASADetialsResponse>(casaResponse); I'm getting this error:
Error mapping types:
Mapping types:
CASADetails -> CASADetialsResponse
AccountManagement.Domain.CASADetails -> AccountManagement.Application.Dtos.CASADetails.CASADetialsResponse
Type Map configuration:
CASADetails -> CASADetialsResponse
AccountManagement.Domain.CASADetails -> AccountManagement.Application.Dtos.CASADetails.CASADetialsResponse
Destination Member:
data
This is how I map the two classes,
public class CASADetailsProfile : Profile
{
public CASADetailsProfile()
{
// Source -> Target
CreateMap<CASADetialsRequest, CASADetails>();
CreateMap<CASADetails, CASADetialsResponse>();
}
}
I just commented the public Data data { get; set; } from the both classes, and without any error its worked. I think problem is with that line. may I know the reason? please help me
The namespace for Data class is different, even though their content is the same.
Delete these
and refer to source Data class namespace or
If you don't want to delete them, add the following configuration
public class CASADetailsProfile: Profile
{
public CASADetailsProfile()
{
// Source -> Target
CreateMap<CASADetialsRequest, CASADetails>();
CreateMap<CASADetails, CASADetialsResponse>();
CreateMap<Sources.Data, Destinations.Data>();
CreateMap<Sources.Account, Destinations.Account>();
CreateMap<Sources.Transaction, Destinations.Transaction>();
CreateMap<Sources.LinkedCard, Destinations.LinkedCard>();
CreateMap<Sources.JointParty, Destinations.JointParty>();
}
}
Change Sources namespace and Destinations namespace to the namespace that you have

Return all derived models in EF Core (TPH )

I have a task which requires me to return all models from a table using inheritance (TPH).
I have a model class called WorkflowInstance and a derived class CustomWorkflowInstance (which has a string property). There is a discriminator of course.
I want to know of a way where I can return all the elements without considering the discriminator
public class WorkflowInstance : Entity, ITenantScope, ICorrelationScope
{
public WorkflowInstance();
public SimpleStack<ActivityScope> Scopes { get; set; }
public SimpleStack<ScheduledActivity> ScheduledActivities { get; set; }
public WorkflowFault? Fault { get; set; }
public HashSet<BlockingActivity> BlockingActivities { get; set; }
public IDictionary<string, IDictionary<string, object?>> ActivityData { get; set; }
public WorkflowOutputReference? Output { get; set; }
public WorkflowInputReference? Input { get; set; }
public Variables Variables { get; set; }
public Instant? FaultedAt { get; set; }
public Instant? CancelledAt { get; set; }
public Instant? FinishedAt { get; set; }
public Instant? LastExecutedAt { get; set; }
public Instant CreatedAt { get; set; }
public string? Name { get; set; }
public string? ContextId { get; set; }
public string? ContextType { get; set; }
public string CorrelationId { get; set; }
public WorkflowStatus WorkflowStatus { get; set; }
public int Version { get; set; }
public string? TenantId { get; set; }
public string DefinitionId { get; set; }
public ScheduledActivity? CurrentActivity { get; set; }
public string? LastExecutedActivityId { get; set; }
}
public class CustomWorkflowInstance : WorkflowInstance
{
public Guid UserId { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<WorkflowInstance>()
.HasDiscriminator<int>("Discriminator")
.HasValue(0)
.HasValue<WorkflowInstance>(0)
.HasValue<CustomWorkflowInstance>(1);}
I want to find a way to query the table as it is meaning adding where clause FinishedAt > etc (the issue is that UserId exist only in derived class but all the data is in base class where discriminator always equals 0)
so by doing select * from WorkflowInstanceTABLE where Used="xx" it automatically adds the where discriminator = 1 (because I wrote _dbContext.CustomWorkflowInstance which contains the userId in question.

asp.net mvc 3 razor Two controllers in one view

Want to use two models in one view. I have two controllers one for current user
public class ProfileModel
{
public int ID { get; set; }
public decimal Balance { get; set; }
public decimal RankNumber { get; set; }
public decimal RankID { get; set; }
public string PorfileImgUrl { get; set; }
public string Username { get; set; }
}
and second for firends
public class FriendsModel
{
public int ID { get; set; }
public string Name { get; set; }
public string ProfilePictureUrl { get; set; }
public string RankName { get; set; }
public decimal RankNumber { get; set; }
}
Profile model contains always one item and Friend model contains list
I have made new model which contains both models :
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public FriendsModel FriendModel { get; set; }
}
I tried to fill FullProfile model like this
List<FriendsModel> fmList = GetFriendsData(_UserID);
FullProfileModel fullModel = new FullProfileModel();
fullModel.ProfileModel = pm;
fullModel.FriendModel = fmList.ToList();
but visual studio gives an error on .ToList()
error:
Cannot implicitly convert type 'System.Collections.Generic.List<NGGmvc.Models.FriendsModel>' to 'NGGmvc.Models.FriendsModel'
Please advice me something how i can show two models in single view.
p.s. im using mvc3 razor view engine
Thanks
You are trying to set a property of type FriendsModel with a value of List.
public FriendsModel FriendModel { get; set; }
Change to:
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public IList<FriendsModel> FriendModel { get; set; }
}
Correct your ViewModel
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public IList<FriendsModel> FriendModels { get; set; }
}
I think you need collection
public class FullProfileModel
{
public ProfileModel ProfileModel { get; set; }
public List<FriendsModel> FriendModels { get; set; }
}

Entity Framework - Reuse Complex Type

I have an Entity in Code First Entity framework that currently looks like this:
public class Entity
{
// snip ...
public string OriginalDepartment { get; set; }
public string OriginalQueue { get; set; }
public string CurrentDepartment { get; set; }
public string CurrentQueue { get; set; }
}
I would like to create Complex Type for these types as something like this:
public class Location
{
public string Department { get; set; }
public string Queue { get; set; }
}
I'd like to use this same type for both Current and Original:
public Location Original { get; set; }
public Location Current { get; set; }
Is this possible, or do I need to create two complex types CurrentLocation and OriginalLocation?
public class OriginalLocation
{
public string Department { get; set; }
public string Queue { get; set; }
}
public class CurrentLocation
{
public string Department { get; set; }
public string Queue { get; set; }
}
It is supported out of box, you do not need to create two complex types.
You can also configure your complex types explicitely with model builder
modelBuilder.ComplexType<Location>();
To customize column names, you should configure them from parent entity configuration
public class Location
{
public string Department { get; set; }
public string Queue { get; set; }
}
public class MyEntity
{
public int Id { get; set; }
public Location Original { get; set; }
public Location Current { get; set; }
}
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<Location>();
modelBuilder.Entity<MyEntity>().Property(x => x.Current.Queue).HasColumnName("myCustomColumnName");
}
}
This will map MyEntity.Current.Queue to myCustomName column

How do I add another table to my database using Code First on MVC3 and Entity Framework?

I have an MVC3 project I created using the Code First paradigm and it works great. I created a model inheriting from DBContext, added the connection string, and I can write to the table and all that.
I can't seem to figure out how to add another table to the database using Code First, though. Adding another connection string that points to the same database doesn't work. Simply adding another class, creating a DBContext for it, and then adding and saving something to the new context doesn't work.
How do I add a new table to the database automatically using just code? My current web.config looks like this:
<add name="PersonDBContext"
connectionString="Data Source=|DataDirectory|PersonData.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
My working model looks like this:
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public string country { get; set; }
public string gender { get; set; }
}
public class PersonDBContext : DbContext
{
public DbSet<Person> People { get; set; }
}
And finally what I want to create:
public class Dog
{
public int ID { get; set; }
public string name { get; set; }
public string breed { get; set; }
}
public class DogDBContext : DbContext
{
public DbSet<Dog> Dogs { get; set; }
}
Why wouldn't you create single context for both classes?
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public string country { get; set; }
public string gender { get; set; }
}
public class Dog
{
public int ID { get; set; }
public string name { get; set; }
public string breed { get; set; }
}
public class ApplicationNameDBContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Dog> Dogs { get; set; }
}
Just use:
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public string country { get; set; }
public string gender { get; set; }
}
public class Dog
{
public int ID { get; set; }
public string name { get; set; }
public string breed { get; set; }
}
public class PersonDBContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Dog> Dogs { get; set; }
}

Categories