如何在 EF Core 中包含导航属性

时间:2021-01-07 06:42:00

标签: c# asp.net-core entity-framework-core asp.net-core-5.0

我使用的是 EF Core 5.0.1。我有以下型号。我想在插入数据后检索导航属性,但总是返回 null。如果我调用“GetStockItemAsync”函数,它会返回导航值。

在成功插入后,我尝试在“AddStockItemAsync”方法中调用“GetStockItemAsync”,该方法也为导航返回 null。

我在我的 asp.net 核心项目中启用了延迟加载代理。

public class StockType : MyDbObject
    {
        public StockType()
        {
            StockItems = new HashSet<StockItem>();
        }

        public int Id { get; set; }
        public string Type { get; set; }
        public string Description { get; set; }

        public virtual ICollection<StockItem> StockItems { get; set; }
    }
public class StockItem : MyDbObject
{
    public long Id { get; set; }
    public int TypeId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual StockType Type { get; set; }
}
public interface IMyDbContext
{
        DbSet<StockType> StockTypes { get; set; }
        DbSet<StockItem> StockItems { get; set; }
        Task<TEntity> InsertAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = default) where TEntity : MyDbObject;
        void Create<TEntity>(TEntity entity) where TEntity : MyDbObject;
        Task<TEntity> GetFirstOrDefaultAsync<TEntity>(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default) where TEntity : MyDbObject;
        Task CommitAsync(CancellationToken cancellationToken = default);
}

public class MyDbContext : IdentityDbContext<User, Role, Guid>, IMyDbContext
{
        public MyDbContext (DbContextOptions<MyDbContext> options) : base(options)
        { }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //required mapping configuration added//
        }

        public DbSet<StockType> StockTypes { get; set; }
        public DbSet<StockItem> StockItems { get; set; }
        public async Task<TEntity> InsertAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = default) where TEntity : MyDbObject
        {
            var savedEntity = await Set<TEntity>().AddAsync(entity, cancellationToken);
            return savedEntity.Entity;
        }
        public void Create<TEntity>(TEntity entity) where TEntity : MyDbObject
        {
            Set<TEntity>().Add(entity);
        }

        public async Task<TEntity> GetFirstOrDefaultAsync<TEntity>(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default) where TEntity : MyDbObject
        {
            return await Set<TEntity>().Where(expression).FirstOrDefaultAsync(cancellationToken);
        }
        public async Task CommitAsync(CancellationToken cancellationToken = default)
        {
            await SaveChangesAsync(cancellationToken);
        }
}

public interface IStockItemServices
    {
        Task<StockItem> GetStockItemAsync(Expression<Func<StockItem, bool>> expression);
        Task AddStockItemAsync(StockItem stockItem);
    }
public class StockItemServices : IStockItemServices
    {
        private readonly IMyRestaurantContext _context;
        public StockItemServices(IMyDbContext context)
        {
            _context = context;
        }

        public async Task AddStockItemAsync(StockItem stockItem)
        {
            _context.Create(stockItem);
            await _context.CommitAsync();
        }
        
        public async Task<StockItem> GetStockItemAsync(Expression<Func<StockItem, bool>> expression) => await _context.GetFirstOrDefaultAsync(expression);
    }

Startup.cs 设置:

services.AddDbContext<MyDbContext>(options =>
            {
                options.UseSqlServer(configuration.GetConnectionString("DbConnectionString"));
                options.UseLazyLoadingProxies(true);
            });

0 个答案:

没有答案