mapping child object in automapper - c#

I have tow classes ( RolesDTO and Roles) in RolesDTO i have List<int> idFonction and in Roles i have List<functions> functions :
Class RolesDTO
public class RolesDTO
{
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<int> IdFunctions { get; set; }
}
Class Role
public partial class Roles
{
public Roles()
{
this.Functions = new HashSet<Functions>();
}
public int Id { get; set; }
public string Description { get; set; }
public virtual ICollection<Functions> Functions { get; set; }
}
How can i mapping List<int> idFunctions to List<funtions> functions with automapper ?
class function
public partial class Functions
{
public int Id { get; set; }
public string Description { get; set; }
public bool Actif { get; set; }
}

Related

C# find child class via parent class

I have 3 classes PropiedadEspecificaFilm, PropiedadEspecificaBook and PropiedadEspecificaMusic which inherit from Product.
public partial class PropiedadEspecificaBook : Product
{
public string ISBN { get; set; }
public string author { get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class PropiedadEspecificaMusic : Product
{
public string name { get; set; }
public string artist{ get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class PropiedadEspecificaMusic : Product
{
public string name { get; set; }
public string artist{ get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class Product
{
public Product()
{
this.OrderLines = new HashSet<OrderLine>();
this.Comments = new HashSet<Comment>();
}
public long id { get; set; }
public string productName { get; set; }
public decimal price { get; set; }
public System.DateTime releaseDate { get; set; }
public short stock { get; set; }
public long categoryId { get; set; }
}
Is there a way to know which one of the PropiedadEspecifica has the productId I am looking for?
Yes, if all products share a property you should declare that in the parent class and not in the children. The children should only have the properties that differ from the parent class.
I also strongly suspect you do not need to mark your classes as partial (that is used if you want to split your class definition up, it is not intended for inheritance).
I also changed so the public properties are capitalized since that is the general convention for c# naming.
public class PropiedadEspecificaBook : Product
{
public string ISBN { get; set; }
public string Author { get; set; }
}
public class PropiedadEspecificaMusic : Product
{
public string Name { get; set; }
public string Artist{ get; set; }
}
public class PropiedadEspecificaMusic : Product
{
public string Name { get; set; }
public string Artist{ get; set; }
}
public class Product
{
public Product()
{
this.OrderLines = new HashSet<OrderLine>();
this.Comments = new HashSet<Comment>();
}
public long Id { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
public System.DateTime ReleaseDate { get; set; }
public short Stock { get; set; }
public long CategoryId { get; set; }
public long ProductId { get; set; }
}
I solved it by adding a virtual Dictionary<String, String> FindPropiedadEspecificaMethod() in Product. And then Overriding in each PropiedadEspecifica. Sorry because the problem was bad explained.

Eagerly loading multiple levels

What could be wrong here? I have seen others include multilevel class to Lambda expression using Select, but I'm getting this error. Can anyone help please?
public partial class Employee
{
public Employee()
{
EmployeeRole = new HashSet<EmployeeRole>();
}
public int Id { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public virtual ICollection<EmployeeRole> EmployeeRole { get; set; }
}
public partial class EmployeeRole
{
public int EmployeeId { get; set; }
public int RoleId { get; set; }
public virtual Employee Employee { get; set; }
public virtual Role Role { get; set; }
}
public partial class Role
{
public Role()
{
EmployeeRole = new HashSet<EmployeeRole>();
}
public int Id { get; set; }
public string Role1 { get; set; }
public virtual ICollection<EmployeeRole> EmployeeRole { get; set; }
}
public async Task<IActionResult> Index()
{
return View(await _context.Employee.Include(a=>a.EmployeeRole.Select(r=>r.Role)).ToListAsync());
}

How to check whether an object has certain method/property of particular type within it?

I have DInfo class present in two different namespaces i.e. ABC.Domain and ABC.Common
I am getting xml body as a record from database from which I am deserializing to respective type.
I have to find out all the records which are using properties with name/names of properties of type ABC.Domain.DInfo and just ignore of type ABC.Common.DInfo
As a I am getting record of type IEvent i.e may be FSubmitted or GSubmitted
namespace ABC.Domain
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
namespace ABC.Common
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
public class Event : IEvent
{
public Guid Id { get; set; }
public Event() { }
public int Number { get; set; }
}
public interface IEvent : IRBase
{
Guid Id { get; set; }
int Number { get; set; }
}
public interface IRBase
{
string RUser { get; set; }
string Sub { get; set; }
}
public abstract class REventBase : Event
{
public Guid Id { get; set; }
}
public class FSubmitted : REventBase
{
public RSummary NewForm { get; set; }
}
public class GSubmitted : REventBase
{
public FRef NewForm { get; set; }
}
public class RSummary
{
public Guid ID { get; set; }
public FRef FRef { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Common.DInfo Saved { get; set; }
public ABC.Domain.DInfo Signed { get; set; }
}
public class FRef : NIdentifier<Guid>
{
public FormType Type { get; set; }
public Version Version { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Domain.DInfo Saved { get; set; }
}
As long as I understand you, you want to get the namespace of the object and then decide what to do with it. If this is the case, this should help.

DbSet Ordering by non primitive field

I'm trying to implement an sorting table in MVC with Razor but i'm having such a pain to make the sorting work with some fields which are non primitive. My class model is as follows
public class Turma
{
public int ID { get; set; }
public string Ano { get; set; }
[ForeignKey("Professor")]
public int Professor_ID { get; set; }
public virtual Professor Professor { get; set; }
[ForeignKey("Disciplina")]
public int Disciplina_ID { get; set; }
public virtual Disciplina Disciplina { get; set; }
}
public class Professor
{
public int ID { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
}
public class Disciplina
{
public int ID { get; set; }
public string Nome { get; set; }
}
I'm tring to, with an DbSet, sorting it like this:
turmas = turmas.OrderByDescending(s => s.Disciplina.Nome);
Is that even possible?

Entity framework CF model structure

I've been working on this for a while now and i still haven't come up with a good idea.
I have the fallowing 3 types Project, User and Result. A user and a project can have multiple results. My problem now is that a result can be of multiple types (12 right now and that can change). I have no idea how i'm supposed to structure this the only things that this results have in common is a type and an Id and i want a table for each of the different 12 types.
So what i have until now is this.
Project
public class Project :ITypedEntity<ProjectType>
{
public int Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
public virtual ProjectType Type { get; set; }
public string TitleEn { get; set; }
public string TitleRo { get; set; }
public string Acronym { get; set; }
public string ContractNo { get; set; }
public virtual ActivityField ActivityField {get;set;}
public string SummaryEn { get; set; }
public string SumarryRo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Value { get; set; }
public bool IsPartner { get; set; }
public ICollection<User> Team{get;set;}
public string Website {get;set;}
public ICollection<INamedEntity> Results;
}
Result
public class Result:ITypedEntity<ResultType>
{
public int Id { get; set; }
public string Name{get;set;}
public ResultType Type { get; set; }
}
User
public class User:Person
{
public int CNP { get; set; }
public virtual Faculty Faculty { get; set; }
public bool IsUSAMV {get;set;}
public virtual ICollection<INamedEntity> Results {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
I don't think this helps but i'll paste them anyway
public interface INamedEntity:IEntity
{
string Name{get;set;}
}
public interface ITypedEntity<TType>:INamedEntity{
TType Type { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
Update 1
Some results types
public class BookChapter:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Book Book {get;set;}
public int PagesInChapter {get;set;}
public string Pagination {get;set;}
}
public class Book:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Person> Authors { get; set; }
public int Year { get; set; }
public Publisher Publisher {get;set;}
public int NumberOfPages { get; set; }
public string Pagination { get; set; }
public int ISBN { get; set; }
}
public class Patent:ITypedEntity<PatentType>
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Authors { get; set; }
public virtual Person Holder{get;set;}
public string Number{get;set;}
public virtual PatentType Type { get; set; }
}
Sorry if the question is not clear, i'll update it with any other information you need.
Thanks.

Categories

Resources