EF Core Include a view - c#

I have the following relationships:
public class File
{
public string Path { get; set; }
}
public class Company
{
public string Name { get; set; }
}
public class Account
{
public int CompanyId { get; set; }
public Company Company { get; set; }
public int FileId { get; set; }
public File Avatar { get; set; }
}
public class Subject
{
public int AttachmentId { get; set; }
public File Attachment { get; set; }
}
public class Collaboration
{
public int SenderId { get; set; }
public Account Sender { get; set; }
public int ReceiverId { get; set; }
public Account Receiver { get; set; }
public int SubjectId { get; set; }
public Subject Subject { get; set; }
}
And I end up with this query
_dataContext
.Collaborations
.AsNoTracking()
.Include(x => x.Sender)
.ThenInclude(x => x.Company)
.Include(x => x.Sender)
.ThenInclude(x => x.Avatar)
.Include(x => x.Receiver)
.ThenInclude(x => x.Company)
.Include(x => x.Receiver)
.ThenInclude(x => x.Avatar)
.Include(x => x.Subject)
.ThenInclude(x => x.Attachment);
I'd like to know if it is possible to replace some of the Include / ThenInclude by a view using EF Core.
And if possible use it as
.Include(x => x.SenderAndCompanyInfo)

First create a sql view;
create view SenderAndCompany as
select s.Id,s.Name, s.SurName,c.CompanyName
from Sender s inner join Company c on c.Id = s.CompanyId
After creating sql view create model for it
public class SenderAndCompany
{
public int Id { get; set; }
public string Name { get; set; }
public string SurName { get; set; }
public string CompanyName { get; set; }
}
//Add to context
public DbSet<SenderAndCompany> SenderAndCompany { get; set; }
//map entity to view within the DbContext.OnModelCreating method
modelBuilder
.Entity<SenderAndCompany>()
.ToView(nameof(SenderAndCompany))
.HasKey(t => t.Id);
//Finally
_dataContext
.Collaborations
.AsNoTracking()
.Include(x => x.Sender)
.ThenInclude(x => x.SenderAndCompany)
.Include(x => x.Receiver)
.ThenInclude(x => x.Avatar)
.Include(x => x.Subject)
.ThenInclude(x => x.Attachment);

Related

Entity Framework Core 7.0 with postgresql [duplicate]

I have an order entity, inside it contains several entities Customer, Store and others, but Entity Framework does not fill those entities. I thought the relationships were wrong but I can't find why the entity framework does not map the entities within orders.
Orders and Customers entities:
public partial class Orders
{
public Orders()
{
OrderItems = new HashSet<OrderItems>();
}
public int OrderId { get; set; }
public int? CustomerId { get; set; }
public byte OrderStatus { get; set; }
public DateTime OrderDate { get; set; }
public DateTime RequiredDate { get; set; }
public DateTime? ShippedDate { get; set; }
public int StoreId { get; set; }
public int StaffId { get; set; }
public virtual Customers Customer { get; set; }
public virtual Staffs Staff { get; set; }
public virtual Stores Store { get; set; }
public virtual ICollection<OrderItems> OrderItems { get; set; }
}
public partial class Customers
{
public Customers()
{
Orders = new HashSet<Orders>();
}
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual ICollection<Orders> Orders { get; set; }
}
Entity Framework code fragment:
modelBuilder.Entity<Orders>(entity =>
{
entity.HasKey(e => e.OrderId)
.HasName("PK__orders__46596229F9C56686");
entity.ToTable("orders", "sales");
entity.Property(e => e.OrderId).HasColumnName("order_id");
entity.Property(e => e.CustomerId).HasColumnName("customer_id");
entity.Property(e => e.OrderDate)
.HasColumnName("order_date")
.HasColumnType("date");
entity.Property(e => e.OrderStatus).HasColumnName("order_status");
entity.Property(e => e.RequiredDate)
.HasColumnName("required_date")
.HasColumnType("date");
entity.Property(e => e.ShippedDate)
.HasColumnName("shipped_date")
.HasColumnType("date");
entity.Property(e => e.StaffId).HasColumnName("staff_id");
entity.Property(e => e.StoreId).HasColumnName("store_id");
entity.HasOne(d => d.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.CustomerId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK__orders__customer__0F4872E8");
entity.HasOne(d => d.Staff)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StaffId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__orders__staff_id__1130BB5A");
entity.HasOne(d => d.Store)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StoreId)
.HasConstraintName("FK__orders__store_id__103C9721");
});
Controller code:
Response in Postman:
One of the options is to eager load them via Include:
return await _context.Orders
.Include(o => o.Customer)
.Include(o => o.Staff)
.Include(o => o.Store)
.ToListAsync()
For more info and options please check out the "Loading Related Data" doc.

Why does the entity framework not map the customer entity in my order?

I have an order entity, inside it contains several entities Customer, Store and others, but Entity Framework does not fill those entities. I thought the relationships were wrong but I can't find why the entity framework does not map the entities within orders.
Orders and Customers entities:
public partial class Orders
{
public Orders()
{
OrderItems = new HashSet<OrderItems>();
}
public int OrderId { get; set; }
public int? CustomerId { get; set; }
public byte OrderStatus { get; set; }
public DateTime OrderDate { get; set; }
public DateTime RequiredDate { get; set; }
public DateTime? ShippedDate { get; set; }
public int StoreId { get; set; }
public int StaffId { get; set; }
public virtual Customers Customer { get; set; }
public virtual Staffs Staff { get; set; }
public virtual Stores Store { get; set; }
public virtual ICollection<OrderItems> OrderItems { get; set; }
}
public partial class Customers
{
public Customers()
{
Orders = new HashSet<Orders>();
}
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual ICollection<Orders> Orders { get; set; }
}
Entity Framework code fragment:
modelBuilder.Entity<Orders>(entity =>
{
entity.HasKey(e => e.OrderId)
.HasName("PK__orders__46596229F9C56686");
entity.ToTable("orders", "sales");
entity.Property(e => e.OrderId).HasColumnName("order_id");
entity.Property(e => e.CustomerId).HasColumnName("customer_id");
entity.Property(e => e.OrderDate)
.HasColumnName("order_date")
.HasColumnType("date");
entity.Property(e => e.OrderStatus).HasColumnName("order_status");
entity.Property(e => e.RequiredDate)
.HasColumnName("required_date")
.HasColumnType("date");
entity.Property(e => e.ShippedDate)
.HasColumnName("shipped_date")
.HasColumnType("date");
entity.Property(e => e.StaffId).HasColumnName("staff_id");
entity.Property(e => e.StoreId).HasColumnName("store_id");
entity.HasOne(d => d.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.CustomerId)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK__orders__customer__0F4872E8");
entity.HasOne(d => d.Staff)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StaffId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__orders__staff_id__1130BB5A");
entity.HasOne(d => d.Store)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.StoreId)
.HasConstraintName("FK__orders__store_id__103C9721");
});
Controller code:
Response in Postman:
One of the options is to eager load them via Include:
return await _context.Orders
.Include(o => o.Customer)
.Include(o => o.Staff)
.Include(o => o.Store)
.ToListAsync()
For more info and options please check out the "Loading Related Data" doc.

Entity Framework Core v3 : Eliminating repeating data

I am using Entity Framework Core v3. I am currently trying to retrieve data from tables that contain a one-to-many relationship.
The tables here are PersonNote and PersonNoteAttachment.
One PersonNote can have many PersonNoteAttachments. Data is getting currently repeated. So if you see the result section it has Name appearing twice. This is just an example. I am not sure if I need to change the query or change the model structure etc. Could somebody help
PersonNote
Id Name
-------------
113 TestNote
PersonNoteAttachment
Id PersonNoteId Note
---------------------------------
101 113 Attachment1
102 113 Attachment2
Result
TestNote Attachment1
TestNote Attachment2
What I am looking at is
TestNote Attachment1
Attachment2
Query
public IQueryable<PersonNote> GetPersonNotes(int personId)
{
var personNotes = _context.PersonNotes
.Include(x => x.Person)
.Include(x => x.Author)
.Include(x => x.PersonNoteAttachment)
.Where(p => p.PersonId == personId)
.OrderByDescending(d => d.Created);
return personNotes;
}
Model
namespace Organisation.Models.DataModels
{
[Table(nameof(PersonNote), Schema = "common")]
public class PersonNote
{
public int Id { get; set; }
public int PersonId { get; set; }
[ForeignKey("PersonId")]
public Person Person { get; set; }
public string Note { get; set; }
public int AuthorId { get; set; }
public PersonNoteAttachment PersonNoteAttachment { get; set; }
[ForeignKey("AuthorId")]
public Person Author { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordStartDateTime { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordEndDateTime { get; set; }
}
public class PersonNoteAttachment
{
public int Id { get; set; }
public int PersonNoteId { get; set; }
[ForeignKey("PersonNoteId")]
public PersonNote PersonNote { get; set; }
public string Alias { get; set; }
public string FileName { get; set; }
public string MimeType { get; set; }
public int Deleted { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordStartDateTime { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime RecordEndDateTime { get; set; }
}
Mapping
CreateMap<Organisation.Models.DataModels.PersonNote, Organisation.Models.User.PersonNote>()
.ForMember(t => t.Id, opt => opt.MapFrom(s => s.Id))
.ForMember(t => t.PersonId, opt => opt.MapFrom(s => s.PersonId))
.ForMember(t => t.AuthorName, opt => opt.MapFrom(s => s.Author.FirstName + " " + s.Author.LastName))
.ForMember(t => t.FileName, opt => opt.MapFrom(s => s.PersonNoteAttachment.FileName))
.ForMember(t => t.MimeType, opt => opt.MapFrom(s => s.PersonNoteAttachment.MimeType))
.ForMember(t => t.Alias, opt => opt.MapFrom(s => s.PersonNoteAttachment.Alias))
.ForMember(t => t.Note, opt => opt.MapFrom(s => s.Note))
.ForMember(t => t.AuthorId, opt => opt.MapFrom(s => s.AuthorId))
.ForMember(t => t.CreatedBy, opt => opt.MapFrom(s => s.CreatedBy))
.ForMember(t => t.Created, opt => opt.MapFrom(s => s.Created));
If you notice PersonNoteAttachment is not an array. If I make it an array, then the following changes would need to be done:
public PersonNoteAttachment[] PersonNoteAttachment { get; set; }
and in the mapping to avoid compile errors
.ForMember(t => t.FileName, opt => opt.MapFrom(s => s.PersonNoteAttachment[0].FileName))
API
[FunctionName(nameof(GetPersonNote))]
[UsedImplicitly]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "person-note/{id}")] HttpRequest req,
int id) => _helper.HandleAsync(async () =>
{
// await _helper.ValidateRequestAsync(req, SecurityPolicies.ViewNotes);
var personNotes = await _organisationRepository.GetPersonNotes(id).ProjectTo<PersonNote>(_mapper.ConfigurationProvider).ToListAsync();
return new OkObjectResult(personNotes);
});
ViewModel
namespace Organisation.Models.User
{
[Table(nameof(PersonNote), Schema = "common")]
public class PersonNote
{
public int Id { get; set; }
public int PersonId { get; set; }
public string Note { get; set; }
public int AuthorId { get; set; }
public string Alias { get; set; }
public string FileName { get; set; }
public string MimeType { get; set; }
public string AuthorName { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
}
}
Solution 1 - But with internal exception
Unable to cast object of type 'System.Boolean' to type 'System.Int32'.
Tried the following , which is providing the desired results . The postman returns 200k. Although debugging the code throwing an internal exception.
Added
Datamodel
namespace Organisation.Models.DataModels
{
public class PersonNote
{
public IEnumerable<PersonNoteAttachment> PersonNoteAttachment { get; set; }
}
}
Viewmodel
namespace Organisation.Models.User
{
[Table(nameof(PersonNote), Schema = "common")]
public class PersonNote
{
public IEnumerable<string> Alias { get; set; }
public IEnumerable<string> FileName { get; set; }
public IEnumerable<string> MimeType { get; set; }
}
}
Mapping
.ForMember(t => t.FileName, opt => opt.MapFrom(s => s.PersonNoteAttachment.Select(x=> x.FileName)))
.ForMember(t => t.MimeType, opt => opt.MapFrom(s => s.PersonNoteAttachment.Select(x => x.MimeType)))
.ForMember(t => t.Alias, opt => opt.MapFrom(s => s.PersonNoteAttachment.Select(x => x.Alias)))
Abduls solution
API
[FunctionName(nameof(GetPersonNote))]
[UsedImplicitly]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "person-note/{id}")] HttpRequest req,
int id) => _helper.HandleAsync(async () =>
{
await _helper.ValidateRequestAsync(req, SecurityPolicies.ViewNotes);
var personNotes = _organisationRepository.GetPersonNotes(id);
//IEnumerable<PersonNote> personNotes = groupedNotes.Select(Profiles.GenistarUserProfile.MapGroupToPersonNote);
return new OkObjectResult(personNotes.ToList());
});
Repository
public IQueryable<Genistar.Organisation.Models.User.PersonNote> GetPersonNotes(int personId)
{
var personNotes = _context.PersonNotes
.Include(x => x.Person)
.Include(x => x.Author)
.Where(p => p.PersonId == personId).Select(x => new Genistar.Organisation.Models.User.PersonNote
{
//assign all properties
Attachments = _context.PersonNotesAttachments.Where(y => y.PersonNoteId == x.Id).Select(y => new Genistar.Organisation.Models.User.PersonNoteAttachment
{
FileName = y.FileName,
Alias = y.Alias,
MimeType = y.MimeType
}),
PersonId = x.PersonId,
AuthorName = x.Person.FirstName + " " + x.Person.LastName,
Note = x.Note,
Id = x.Id,
Created = x.Created
}).OrderByDescending(x=> x.Created).AsNoTracking();
return personNotes;
}
The following code will allow you to use a 3 layered architecture with your app, and try to reduce the coupling between your layers while keeping your existing structure.
To start, change your query:
public IQueryable<DataModel.PersonNote> GetPersonNotes(int personId)
{
var personNotes = _context.PersonNotes
.Include(x => x.Person)
.Include(x => x.Author)
.Where(p => p.PersonId == personId);
return personNotes;
}
And add another repo query:
public IQueryable<DataModel.PersonNoteAttachment> GetAttachments(int personNoteId)
{
var attachments = _context.PersonNotesAttachments.Where(x => x.Id == personNoteId);
return attachments ;
}
Use the following Models, PersonNote being your ViewModel:
namespace Organisation.Models.User
{
public class PersonNoteAttachment
{
public string Alias { get; set; }
public string FileName { get; set; }
public string MimeType { get; set; }
}
public class PersonNote
{
public int Id { get; set; }
public int PersonId { get; set; }
public string Note { get; set; }
public int AuthorId { get; set; }
public IEnumerable<PersonNoteAttachment> Attachments { get; set; }
public string AuthorName { get; set; }
public string CreatedBy { get; set; }
public DateTime Created { get; set; }
}
}
Now we create a new function in BL layer that will return your data for the ViewModel:
public IEnumerable<User.PersonNote> GetPersonNotesAndAttachments(int personID)
{
var personNotes = _organisationRepository.GetPersonNotes(id).Select(x =>
new User.PersonNote
{
//assign all properties
Attachments = _organisationRepository.GetAttachments(x.PersonNoteID).Select(y =>
new User.PersonNoteAttachment
{
FileName = y.FileName,
Alias = y.Alias,
MimeType = y.MimeType
}).AsEnumerable(),
PersonId = x.PersonId,
AuthorName = x.Person.FirstName + " " + x.Person.LastName,
Note = x.Note,
Id = x.Id,
Created = x.Created
});
return personNotes;
}
The IEnumerable<PersonNote> "What I am looking at is" can be accessed like this:
IEnumerable<User.PersonNote> personNotes = new BlClass().GetPersonNotesAndAttachments(id);

Using Contains() on Included entity

I have the following code:
public PaginatedList<PdModel> PdModel { get; set; }
public async Task OnGetAsync(int id, int? pageIndex, string searchString)
{
IQueryable<PdModel> PdModelsQuer = _context.PdModel.Where(x => x.Id == id)
.Include(x => x.PdTables)
.Include(x => x.pdFolderTree)
.Include(x => x.PdReferences.Where(y=>y.ReferenceName.Contains(searchString)))
.Include(x => x.pdViews)
.Include(x => x.pdDomains)
.Include(x => x.PdModelSources)
.Include(x => x.pdModelExtendeds)
.Include(x => x.pdRules);
PdModel = await PaginatedList<PdModel>.CreateAsync(PdModelsQuer, 1, 10);
}
On code execution I am getting this error:
InvalidOperationException: The property expression 'x => {from PdReference y in x.PdReferences where [y].ReferenceName.Contains(__searchString_1) select [y]}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
I guess I have to use Contains() on included property in another way. I tried a lot of things, but no reasonable code seems to be working.
Anybody can help me on this?
Thanks a lot in advance
My Domain models:
public class PdModel
{
[Key]
public int Id { get; set; }
public string ModelCode { get; set; }
public string ModelName { get; set; }
public string ModelComment { get; set; }
public string ModelDescription { get; set; }
public string ModelAnnotation { get; set; }
public string ModelDatabase { get; set; }
public DateTime? ModelCreationDate { get; set; }
public string ModelCreationUser { get; set; }
public DateTime? ModelModificationDate { get; set; }
public string ModelModificationUser { get; set; }
public string ModelGarantExtendedFlag { get; set; }
public string ModelColumnExtendedFlag { get; set; }
public string ModelTableExtendedFlag { get; set; }
public DateTime PdInsertedDate { get; set; }
public ICollection<PdRule> pdRules { get; set; }
public ICollection<PdModelExtended> pdModelExtendeds {get;set;}
public ICollection<PdTable> PdTables { get; set; }
public ICollection<PdReference> PdReferences { get; set; }
public ICollection<PdModelSource> PdModelSources { get; set; }
public ICollection<PdDomain> pdDomains { get; set; }
public ICollection<PdView> pdViews { get; set; }
[ForeignKey("Id")]
public virtual PdFolderTree pdFolderTree { get; set; }
}
public class PdReference
{
public int Id { get; set; }
public int ModelId { get; set; }
public string ModelCode { get; set; }
public string ReferenceCode { get; set; }
public string ReferenceName { get; set; }
public string ReferenceComment { get; set; }
public string ReferenceDescription { get; set; }
public string ReferenceAnnotation { get; set; }
public string ReferenceStereotype { get; set; }
public int ParentModelId { get; set; }
public string ParentModelCode { get; set; }
public string ParentTableCode { get; set; }
public int ParentTableId { get; set; }
public int ChildTableId { get; set; }
public string ChildTableCode { get; set; }
public string Cardinality { get; set; }
public DateTime PdInsertedDate { get; set; }
[ForeignKey("ModelId")]
public PdModel PdModels { get; set; }
public ICollection<PdJoin> pdJoins { get; set; }
[ForeignKey("ChildTableId")]
public virtual PdTable pdChildTable { get; set; }
You cannot filter an eagerly loaded relationship. The error you're getting is due to Include needing to be passed a valid property expression, which a Where clause is not.
If you only want to load a subset of this particular relationship, you'll need to explicitly load it. For example:
IQueryable<PdModel> PdModelsQuer = _context.PdModel.Where(x => x.Id == id)
.Include(x => x.PdTables)
.Include(x => x.pdFolderTree)
// remove this .Include(x => x.PdReferences.Where(y=>y.ReferenceName.Contains(searchString)))
.Include(x => x.pdViews)
.Include(x => x.pdDomains)
.Include(x => x.PdModelSources)
.Include(x => x.pdModelExtendeds)
.Include(x => x.pdRules);
foreach (var pdModel in PdModelsQuer)
{
var pdReferences = await _context.Entry(pdModel).Collection(x => x.PdReferences).Query()
.Where(x = x.ReferenceName.Contains(searchString)).ToListAsync();
}
If it's not obvious, this means issuing N+1 queries, where N is the count of your PdModels. In other words, the filtered collection has to be fetched for each instance individually.
However, based on querying by id, it appears that you should only have one matching PdModel. As such, you really shouldn't be using a Where here. Instead. Just add all your includes and then use SingleOrDefaultAsync:
var pdModel = await _context.PdModel
.Include(x => x.PdTables)
.Include(x => x.pdFolderTree)
.Include(x => x.pdViews)
.Include(x => x.pdDomains)
.Include(x => x.PdModelSources)
.Include(x => x.pdModelExtendeds)
.Include(x => x.pdRules)
.SingleOrDefaultAsync(x => x.Id == id);
Then, you can fetch the PdReferences for just this one instance:
var pdReferences = await _context.Entry(pdModel).Collection(x => x.PdReferences).Query()
.Where(x = x.ReferenceName.Contains(searchString)).ToListAsync();
It's important to note that this is being stored in another variable. Setting the filtered collection directly to your PdReferences property can cause side-effects, particularly if you end up trying to save this entity later, namely removing anything not in the filtered list from the database. In a situation like this, it's best to employ a view model and map over the data accordingly.

EF6 fluent one-to-one stackoverflow

I have read a lot of posts and some helped me, but I still have a stackoverflow on a one-to-one relation (between Employe and Adresse) defined with Entity framework 6 fluent.
The error appears on AdresseDTO when I create a new instance of a ViewModel wich contains some DTOs.
My plain objects
public class Personne
{
public int id { get; set; }
public string civ { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public string email { get; set; }
public string tel1 { get; set; }
public string tel2 { get; set; }
public Boolean isFournisseur { get; set; }
public Boolean isClient { get; set; }
public Boolean isDeleted { get; set; }
public virtual Adresse adresse { get; set; }
public virtual Utilisateur utilisateur { get; set; }
public Personne()
{
}
}
public class Employe : Personne
{
public virtual TEmploye typeEmploye { get; set; }
public virtual List<AffectationService> affectationServices { get; set; }
public Employe()
{
}
}
public class AffectationService
{
public int id { get; set; }
public virtual Employe employe { get; set; }
public virtual Service service { get; set; }
public virtual Droit groupe { get; set; }
public Boolean isPrincipal { get; set; }
}
public class TEmploye
{
public int id { get; set; }
public string libe { get; set; }
public TEmploye()
{
}
}
public class Adresse
{
public int id { get; set; }
public string numRue { get; set; }
public string nomRue { get; set; }
public string codePostal { get; set; }
public string ville { get; set; }
public string pays { get; set; }
public virtual Personne personne { get; set; }
public Adresse()
{
}
}
My DTOs
public class PersonneDTO
{
public int id { get; set; }
public bool isDeleted { get; set; }
public string civ { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public string email { get; set; }
public string tel1 { get; set; }
public string tel2 { get; set; }
public AdresseDTO adresse { get; set; }
public UtilisateurDTO utilisateur { get; set; }
public PersonneDTO()
{
adresse = new AdresseDTO();
utilisateur = new UtilisateurDTO();
}
}
public class EmployeDTO : PersonneDTO
{
public virtual TEmployeDTO typeEmploye { get; set; }
public virtual List<AffectationServiceDTO> affectationServices { get; set; }
public EmployeDTO()
{
affectationServices = new List<AffectationServiceDTO>();
typeEmploye = new TEmployeDTO();
}
}
public class TEmployeDTO
{
public int id { get; set; }
public string libe { get; set; }
public TEmployeDTO()
{
}
}
public class AffectationServiceDTO
{
public int id { get; set; }
public virtual EmployeDTO employe { get; set; }
public virtual ServiceDTO service { get; set; }
public virtual DroitDTO groupe { get; set; }
public Boolean isPrincipal { get; set; }
public AffectationServiceDTO()
{
service = new ServiceDTO();
groupe = new DroitDTO();
employe = new EmployeDTO();
}
}
public class AdresseDTO
{
public int id { get; set; }
public string numRue { get; set; }
public string nomRue { get; set; }
public string codePostal { get; set; }
public string ville { get; set; }
public string pays { get; set; }
public AdresseDTO()
{
}
}
How I mapp my DTOs with the plain objects
//DomainToViewModel
CreateMap<Adresse, AdresseDTO>().MaxDepth(1);
CreateMap<Personne, PersonneDTO>().MaxDepth(1);
CreateMap<Employe, EmployeDTO>().MaxDepth(1);
CreateMap<AffectationService, AffectationServiceDTO>().MaxDepth(1);
CreateMap<TEmploye, TEmployeDTO>().MaxDepth(1);
//ViewModelToDomain
CreateMap<AdresseDTO, Adresse>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.codePostal, map => map.MapFrom(vm => vm.codePostal))
.ForMember(g => g.nomRue, map => map.MapFrom(vm => vm.nomRue))
.ForMember(g => g.numRue, map => map.MapFrom(vm => vm.numRue))
.ForMember(g => g.pays, map => map.MapFrom(vm => vm.pays))
.ForMember(g => g.ville, map => map.MapFrom(vm => vm.ville));
CreateMap<UtilisateurDTO, Utilisateur>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.dConnexion, map => map.MapFrom(vm => vm.dConnexion))
.ForMember(g => g.dCreation, map => map.MapFrom(vm => vm.dCreation))
.ForMember(g => g.isActive, map => map.MapFrom(vm => vm.isActive))
.ForMember(g => g.isDeleted, map => map.MapFrom(vm => vm.isDeleted))
.ForMember(g => g.login, map => map.MapFrom(vm => vm.login))
.ForMember(g => g.password, map => map.MapFrom(vm => vm.password))
.ForMember(g => g.personne, map => map.MapFrom(vm => vm.personne)).MaxDepth(1);
CreateMap<EmployeDTO, Employe>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.typeEmploye, map => map.MapFrom(vm => vm.typeEmploye))
.ForMember(g => g.affectationServices, map => map.MapFrom(vm => vm.affectationServices))
.ForMember(g => g.utilisateur, map => map.MapFrom(vm => vm.utilisateur)).MaxDepth(1)
.ForMember(g => g.adresse, map => map.MapFrom(vm => vm.adresse)).MaxDepth(1);
CreateMap<TEmployeDTO, TEmploye>().MaxDepth(1)
.ForMember(g => g.libe, map => map.MapFrom(vm => vm.libe));
CreateMap<AffectationServiceDTO, AffectationService>().MaxDepth(1)
.ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
.ForMember(g => g.employe, map => map.MapFrom(vm => vm.employe)).MaxDepth(1)
.ForMember(g => g.groupe, map => map.MapFrom(vm => vm.groupe)).MaxDepth(1)
.ForMember(g => g.isPrincipal, map => map.MapFrom(vm => vm.isPrincipal))
.ForMember(g => g.service, map => map.MapFrom(vm => vm.service)).MaxDepth(1);
Mapping with EF6 fluent
public PersonneConfiguration()
{
ToTable("Personne");
HasKey<int>(a => a.id);
HasRequired<Adresse>(x => x.adresse);
HasOptional<Utilisateur>(x => x.utilisateur);
Property(a => a.civ).HasColumnType("varchar").HasMaxLength(3);
Property(a => a.nom).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.prenom).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.email).HasColumnType("varchar").HasMaxLength(150);
Property(a => a.tel1).HasColumnType("varchar").HasMaxLength(10);
Property(a => a.tel2).HasColumnType("varchar").HasMaxLength(10);
Property<bool>(a => a.isClient);
Property<bool>(a => a.isFournisseur);
Property<bool>(a => a.isDeleted);
}
public EmployeConfiguration()
{
ToTable("Employe");
HasKey<int>(a => a.id);
HasOptional<TEmploye>(x => x.typeEmploye);
Property<bool>(a => a.isDeleted);
}
public AdresseConfiguration()
{
ToTable("Adresse");
HasKey<int>(a => a.id);
Property(a => a.codePostal).HasColumnType("varchar").HasMaxLength(5);
Property(a => a.nomRue).HasColumnType("varchar").HasMaxLength(150);
Property(a => a.numRue).HasColumnType("varchar").HasMaxLength(10);
Property(a => a.ville).HasColumnType("varchar").HasMaxLength(80);
Property(a => a.pays).HasColumnType("varchar").HasMaxLength(80);
}
public AffectationServiceConfiguration()
{
ToTable("AffectationService");
HasKey<int>(a => a.id);
HasRequired<Employe>(x => x.employe).WithMany(x => x.affectationServices);
HasRequired<Service>(x => x.service);
HasRequired<Droit>(x => x.groupe);
Property<bool>(a => a.isPrincipal);
}
public TEmployeConfiguration()
{
ToTable("TEmploye");
HasKey<int>(a => a.id);
Property(a => a.libe).HasColumnType("varchar").HasMaxLength(100);
}
And the ViewModel taht causes the error
public class EditEmployeViewModel
{
public EmployeDTO personne { get; set; }
public EditEmployeViewModel()
{
personne = new EmployeDTO();
}
}
The stackoverflow occurs when a create a new EmployeDTO(). I tried to solve this by adding .MaxDepth(1) where it's possible, but it doesn't work...
Sorry guys, I know that is a long post, but I don't have any idea about my problem so I try to show you the most relevent part of my code.
Thank you for all, I'm brand new in EF6 so if you have any tips or good pratices to share it will be a pleasure.
thank you for your help.
I didnt' find a real solution so I removed the refererence Personne in my object Adresse to avoid the stackoverflow exception...

Categories