Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

c# - How to include navigation properties in EF Core

I am using EF Core 5.0.1. I have the following model. I want to retrieve the navigation property after inserting data, but always return null. If I call "GetStockItemAsync" function it is returning with navigation values.

I tried, after a successful insert, to call "GetStockItemAsync" within "AddStockItemAsync" method, which is also returning null for navigations.

I have enabled lazy loading proxies in my asp.net core project.

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 settings:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...