efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Materialization using Property instead field

Open yarmenteros opened this issue 10 years ago • 6 comments

The issue

I have problems with the materialization of entities used within Include method. E.F. is using the properties to set values if the associated field does not have the _ prefix. This only happens with entities inside the Include method. If I use the same entity based in their DbSet it works fine without using the properties to set the values.

By tradition I use the same property name in my fields starting with a lowercase.

Ex.:

Property: FirstName, Field: firstName

Use the property to set value of person first name DbContext.Employees.Include(x => x.Person).Where(x => ...);

Use the field to set value of person first name: DbContext.Persons.Where(x => ...);


Property: FirstName, Field: _firstName

In both cases use the field to set value of person first name: DbContext.Employees.Include(x => x.Person).Where(x => ...); DbContext.Persons.Where(x => ...);

Thanks for your time and your work... Regards,

yarmenteros avatar Nov 10 '16 20:11 yarmenteros

@yarmenteros Can you provide a more complete listing or project to reproduce the issue? Also, what version of EF Core are you using?

ajcvickers avatar Nov 10 '16 20:11 ajcvickers

I using EF Core 1.0.1

public class CompanyGroup : BindableBase
{
    private string name;
    private bool isActive;

    public CompanyGroup()
    {       
        Sites = new HashSet<CompanyGroupSite>();
    }

    public Guid Id { get; set; }

    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }

    public bool IsActive
    {
        get { return isActive; }
        set { SetProperty(ref isActive, value); }
    }

    public virtual ICollection<CompanyGroupSite> Sites { get; set; }
}

public class CompanyGroupSite : BindableBase
{
    private bool isActive;
    private Guid siteId;
    private Guid companyGroupId;

    public Guid Id { get; set; }

    public Guid SiteId
    {
        get { return siteId; }
        set { SetProperty(ref siteId, value); }
    }

    public virtual Site Site { get; set; }

    public Guid CompanyGroupId
    {
        get { return companyGroupId; }
        set { SetProperty(ref companyGroupId, value); }
    }

    public bool IsActive
    {
        get { return isActive; }
        set { SetProperty(ref isActive, value); }
    }
}

public class Site : BindableBase
{
    private string name;

    public Guid Id { get; set; }

    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }
}

public class MyDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        //...
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //Mapping...
    }

    public DbSet<CompanyGroup> CompanyGroups { get; set; }
    public DbSet<CompanyGroupSite> CompanyGroupSites { get; set; }
    public DbSet<Site> Sites { get; set; }

}

public class CompanyGroupViewModel : BindableBase
{
    private IEnumerable<CompanyGroup> companyGroupsActiveList;

    public CompanyGroupViewModel()
    {
        Load();
    }

    public IEnumerable<CompanyGroup> CompanyGroupsActiveList
    {
        get { return companyGroupsActiveList; }
        set { SetProperty(ref companyGroupsActiveList, value); }
    }

    private void Load()
    {
        using(var DbContext =  new MyDbContext())
        {
            CompanyGroupsActiveList = DbContext.CompanyGroups.Include(x => x.Sites).ThenInclude(x => x.Site).Where(x => x.IsActive).ToList();
        }
    }
}

yarmenteros avatar Nov 10 '16 20:11 yarmenteros

@yarmenteros Here is a full console application using your code:

public class CompanyGroup : BindableBase
{
    private string name;
    private bool isActive;

    public CompanyGroup()
    {
        Sites = new HashSet<CompanyGroupSite>();
    }

    public Guid Id { get; set; }

    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }

    public bool IsActive
    {
        get { return isActive; }
        set { SetProperty(ref isActive, value); }
    }

    public virtual ICollection<CompanyGroupSite> Sites { get; set; }
}

public class CompanyGroupSite : BindableBase
{
    private bool isActive;
    private Guid siteId;
    private Guid companyGroupId;

    public Guid Id { get; set; }

    public Guid SiteId
    {
        get { return siteId; }
        set { SetProperty(ref siteId, value); }
    }

    public virtual Site Site { get; set; }

    public Guid CompanyGroupId
    {
        get { return companyGroupId; }
        set { SetProperty(ref companyGroupId, value); }
    }

    public bool IsActive
    {
        get { return isActive; }
        set { SetProperty(ref isActive, value); }
    }
}

public class Site : BindableBase
{
    private string name;

    public Guid Id { get; set; }

    public string Name
    {
        get { return name; }
        set { SetProperty(ref name, value); }
    }
}

public class BindableBase
{
    public void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
    {
        Console.WriteLine("Setting " + propertyName);
        field = value;
    }
}

public class MyDbContext : DbContext
{

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=CollectionsTest;Trusted_Connection=True;ConnectRetryCount=0");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //Mapping...
    }

    public DbSet<CompanyGroup> CompanyGroups { get; set; }
    public DbSet<CompanyGroupSite> CompanyGroupSites { get; set; }
    public DbSet<Site> Sites { get; set; }

}

public class CompanyGroupViewModel : BindableBase
{
    private IEnumerable<CompanyGroup> companyGroupsActiveList;

    public CompanyGroupViewModel()
    {
        Load();
    }

    public IEnumerable<CompanyGroup> CompanyGroupsActiveList
    {
        get { return companyGroupsActiveList; }
        set { SetProperty(ref companyGroupsActiveList, value); }
    }

    private void Load()
    {
        using (var DbContext = new MyDbContext())
        {
            CompanyGroupsActiveList = DbContext.CompanyGroups.Include(x => x.Sites).ThenInclude(x => x.Site).Where(x => x.IsActive).ToList();

            foreach (var companyGroup in CompanyGroupsActiveList)
            {
                Console.WriteLine("CompanyGroup " + companyGroup.Id);
                foreach (var companyGroupSite in companyGroup.Sites)
                {
                    Console.WriteLine("  CompanyGroupSite " + companyGroupSite.Id);
                    Console.WriteLine("  Side " + companyGroupSite.Site.Id);
                }
            }
        }
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        using (var context = new MyDbContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var site1 = new Site();
            var cg1 = new CompanyGroupSite {Site = site1};

            var site2 = new Site();
            var cg2 = new CompanyGroupSite { Site = site2 };

            var site3 = new Site();
            var cg3 = new CompanyGroupSite { Site = site3 };

            var cg = new CompanyGroup { IsActive = true };
            cg.Sites.Add(cg1);
            cg.Sites.Add(cg2);
            cg.Sites.Add(cg3);

            context.Add(cg);
            context.SaveChanges();
        }

        Console.WriteLine("Loading...");

        new CompanyGroupViewModel();

    }
}

I ran it first as above and got the following output:

Setting IsActive
Setting CompanyGroupId
Setting SiteId
Setting CompanyGroupId
Setting SiteId
Setting CompanyGroupId
Setting SiteId
Loading...
Setting CompanyGroupsActiveList
CompanyGroup a6803a6d-fb96-4346-e04b-08d40a5b3578
  CompanyGroupSite ebd97b4e-5ddc-44e0-9d7a-08d40a5b357d
  Side 170d4bf1-b5e0-44b5-fd53-08d40a5b357e
  CompanyGroupSite fe63d9ea-06c2-41b1-9d7b-08d40a5b357d
  Side 9aee4869-3940-4435-fd54-08d40a5b357e
  CompanyGroupSite 9fc6b675-8e84-481c-9d7c-08d40a5b357d
  Side 96b80ce2-de1c-4ef0-fd55-08d40a5b357e

I then renamed all the backing fields to have a "_" prefix and ran again. The output was the same.

Could you look at the code and see what changes are needed to reproduce what you are seeing?

ajcvickers avatar Nov 11 '16 17:11 ajcvickers

@ajcvickers you want @yarmenteros , not me. :)

yanchenw avatar Nov 11 '16 17:11 yanchenw

Sorry. :-)

ajcvickers avatar Nov 11 '16 18:11 ajcvickers

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

divega avatar Nov 18 '16 19:11 divega