0

Scenario

public class Product : Entity, IAggregateRoot
{
    public string Name { get; set; }
    public string Dimension { get; set; }
    public decimal Volume { get; set; }
    public bool Featured { get; set; }
    public Farm Farm { get; set; }
    public int FarmId  { get; set; }
    /// <summary>
    /// Sell Price
    /// </summary>
    public decimal BidPrice { get; set; }
    public int QuantityAvaliable { get; set; }
    public ICollection<Image> Images { get; set; }
    public string Description { get; set; }
    public Category Category { get; set; }
    public int CategoryId { get; set; }
    public DateTime Created { get; set; }
    public DateTime? Modified { get; set; }
}

public class Category : Entity, IAggregateRoot
{
    public string Title { get; set; }
    public string CategoryImage { get; set; }
    public Category Parent { get; set; }
    public DateTime Created { get; set; }
    public DateTime? Modified { get; set; }
}

Relationship setup

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
        HasKey(x => x.Id);

        Property(x => x.Created).HasColumnType("DateTime");
        Property(x => x.Modified).HasColumnType("DateTime");
        Property(x => x.BidPrice).HasColumnType("Decimal");

        #region RELATIONSHIP
        //BelongsTo
        HasRequired(x => x.Farm);
        HasRequired(x => x.Category);

        HasMany(x => x.Images);

        #endregion
    }

So I have this two model where I need to bring the data from Product model with Category information

I have checked my database, the data is consistent, the Product record have the FK for the Category record.

but when I try to get Product Data using EF6, the category information doesnt come, I get a null object.

Because of = () =>
    {
        _product = _repository.Find(p => p.Id == 1, p => p.Category);
    };

    It should_not_be_bull = () =>
        _product.Category.ShouldNotBeNull();

the response from data base is for Category is null. but the record is there.

I had it working properly before. for some random magic reason it just stop working.

THE FIND method

public virtual TEntity Find(Expression<Func<TEntity, bool>> predicate = null, params Expression<Func<TEntity, object>>[] includes)
    {
        var set = CreateIncludedSet(includes);

        return (predicate == null) ?
               set.FirstOrDefault() :
               set.FirstOrDefault(predicate);
    }

the CreateIncludeSet

private IDbSet<TEntity> CreateIncludedSet(IEnumerable<Expression<Func<TEntity, object>>> includes)
    {
        var set = CreateSet();

        if (includes != null)
        {
            foreach (var include in includes)
            {
                set.Include(include);
            }
        }

        return set;
    }

the CreateSet method

private IDbSet<TEntity> CreateSet()
    {
        return Context.CreateSet<TEntity>();
    }

MY DbContext implementation is here

https://github.com/RobsonKarls/FreedomWebApi/blob/dev/Source/Freedom.Infrastructure.DataAccess/Factories/FreedomDbContext.cs

all project is there too for further analisys

any help is valuable.

Thank you

RobsionKarls
  • 136
  • 1
  • 9
  • [code]public virtual TEntity Find(Expression> predicate = null, params Expression>[] includes) [/code] The Method take 2 parameters, first is for Where () and Second one is for Include – RobsionKarls Jul 29 '16 at 20:56

2 Answers2

0

Your code is a bit unclear, but try something like this....

_product = _repository.Include(p => p.Category).SingleOrDefault(x => x.Id == 1);

also see... https://stackoverflow.com/a/7348694/6200410

Community
  • 1
  • 1
Nez
  • 59
  • 4
0

The problem in your code is in this line in CreateIncludedSet method:

set.Include(include);

Yes, you include the data but you do not change you set. You should change it to something like:

set = set.Include(include);
Adil Mammadov
  • 8,476
  • 4
  • 31
  • 59
  • set variable is type of IDbSet and set.Include() returns a IQueryable, I tried to do this `set.Expression = set.Include(include)` but Expression has no setter. but i got your point and totally agree with that. – RobsionKarls Aug 01 '16 at 16:02
  • 1
    I got your insight and could solve the problem, I changed Method: `private IQueryable CreateIncludedSet(IEnumerable>> includes) { IQueryable set = CreateSet(); if (includes != null) { foreach (var include in includes) { set = set.Include(include); } } return set; }` Before it used to return a IDbSet and now it Returns a Iqueryable, this way I could do what you told me before. – RobsionKarls Aug 01 '16 at 16:48