EF6忽略相关数据

时间:2016-07-29 20:18:37

标签: entity-framework lazy-loading entity-relationship

方案

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; }
}

关系设置

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
    }

所以我有这两个模型,我需要从Product model带来类别信息

我已经检查了我的数据库,数据是一致的,产品记录有类别记录的FK。

但是当我尝试使用EF6获取产品数据时,类别信息没有来,我得到一个空对象。

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

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

数据库的响应是Category为null。但记录就在那里。

我以前工作得很好。因为一些随机的魔术原因它只是停止工作。

FIND方法

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);
    }

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;
    }

CreateSet方法

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

我的DbContext实现在这里

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

所有项目也都有进一步的分析

任何帮助都很有价值。

谢谢

2 个答案:

答案 0 :(得分:0)

您的代码有点不清楚,但尝试这样的事情......

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

另见...... https://stackoverflow.com/a/7348694/6200410

答案 1 :(得分:0)

代码中的问题出在 CreateIncludedSet 方法的这一行:

set.Include(include);

是的,您包含数据但不会更改您设置。您应该将其更改为:

set = set.Include(include);