Divide List object in two objects with Entity Framework

asp.net-mvc c# ef-code-first entity-framework-6 lazy-loading

Question

I do have a question...
I have the following code that I need to split a List into two objects:

public class User
{
    [Key]
    public Guid Id { get; set; }

    public virtual List<PersonalData> _PersonalData
    {
        get
        {
            List<PersonalData> listDP = new List<PersonalData>();
            if (PersonalData != null)
            {
                listDP.Add(PersonalData);
            }
            if (InitialsPersonalData != null)
            {
                listDP.Add(InitialsPersonalData);
            }
            return listDP;
        }
        set
        {
            InitialsPersonalData = value.FirstOrDefault(dp => dp.IsInitialData);
            PersonalData = value.FirstOrDefault(dp => !dp.IsInitialData);
        }
    }

    [NotMapped]
    public PersonalData InitialsPersonalData { get; set; }
    [NotMapped]
    public PersonalData PersonalData { get; set; }
}

public class PersonalData
{
    [Key, ForeignKey("User"), Column(Order = 0)]
    public Guid User_Id { get; set; }
    [Key, Column(Order = 1)]
    public bool IsInitialData { get; set; }
    public virtual User User { get; set; }
    [Required]
    public string Email { get; set; }
    [Required]
    public string Name { get; set; }
    public string Surname { get; set; }
}

public class Document
{
    [Key, ForeignKey("User"), Column(Order = 0)]
    public Guid User_Id { get; set; }
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.None)]
    public DocumentType DocumentType { get; set; }
    public virtual User User { get; set; }
    public string DocumentNumber { get; set; }
}

Is it even possible? Lazy Loading is what I'm utilising. Never enter the set method; the get method always returns true.
I attempt to utilise "_PersonalData" in both private and public settings, with and without virtualization. The issue arises when I:

dbContext.Users.Where(......)

PersonalData and InitialPersonalData are always null, but Documents are acceptable and do the Include automatically.
Would you kindly assist me? What is the issue? Is it even possible? I appreciate it everything.

1
0
8/23/2018 7:07:37 AM

Popular Answer

PersonalData and InitialPersonalData are not mapped in the DB, so you cannot use them on DB side.

You must make your facts tangible usingdbContext.Users.ToList() prior to utilising them, however depending on the size, this can bring up your complete Users table and slow down your application. If you have any additional criteria to filter your table before materialising, use it to optimise performance.

0
8/23/2018 8:04:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow