Razor组件+ EF Core +存储库/ UoW模式更改实体而没有SaveChanges()

时间:2019-04-01 15:29:45

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

我目前正在使用Razor组件,实体框架核心和一些标准模式(存储库模式,UnitOfWork)构建测试应用程序。我的实体中的更改是临时保存的(如果我浏览页面,则更改可见),而无需在UoW类中调用SaveChanges(错误)。刷新应用程序将删除这些临时文件。更改-调用我的工作单元Complete()函数可将更改的数据永久保存(右)。

我不知道为什么这些临时对象更改在我的页面中使用/可见。工作单元通常是工作的,因为仅调用SaveChanges()会为我的应用运行时保存数据)。当前设置:

  • VS 2019预览版
  • .NET Core v3.0.0-preview2(因为预览版3不能与Windows 7和Razor组件结合使用,因此该问题已在GitHub上指出)

Startup.cs-在服务器启动中添加工作单元

services.AddScoped<IUnitOfWork, UnitOfWork>();

并添加内存数据库上下文

services.AddDbContext<DatabaseContext>(options => options.UseInMemoryDatabase("Database"));

DatabaseContext.cs

public class DatabaseContext : DbContext
    {

        public DbSet<Customer> Customers { get; set; }

        public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
        {



        }

        protected override void OnModelCreating(ModelBuilder builder)
        {

            builder.Entity<Customer>(ConfigureCustomer);

        }

        private void ConfigureCustomer(EntityTypeBuilder<Customer> builder)
        {

            builder.ToTable("Customers");
            builder.HasKey(c => c.Id);
            builder.Property(c => c.Id).IsRequired();

        }

    }

Repository.cs-存储库实现

public class Repository<T> : IRepository<T> where T : class
    {

        protected readonly DbContext Context;

        public Repository(DbContext context)
        {

            Context = context;

        }

        public void Add(T entity)
        {

            Context.Set<T>().AddAsync(entity);

        }

        public void AddRange(IEnumerable<T> entities)
        {

            Context.Set<T>().AddRange(entities);

        }

        public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
        {

            return await Context.Set<T>().Where(predicate).ToListAsync();

        }

        public async Task<T> GetAsync(int id)
        {

            return await Context.Set<T>().FindAsync(id);

        }

        public async Task<IEnumerable<T>> GetAllAsync()
        {

            return await Context.Set<T>().ToListAsync();

        }

        public void Remove(T entity)
        {

            Context.Set<T>().Remove(entity);

        }

        public void RemoveRange(IEnumerable<T> entities)
        {

            Context.Set<T>().RemoveRange(entities);

        }

        public void Update(T entity)
        {

            Context.Set<T>().Update(entity);

        }


    }

CustomersRepository.cs

public class CustomersRepository : Repository<Customer>, ICustomersRepository
    {
        public CustomersRepository(DatabaseContext context) : base(context)
        {



        }

    }

UnitOfWork实现

public class UnitOfWork : IUnitOfWork
    {

        public ICustomersRepository Customers { get; private set; }

        private readonly DatabaseContext _context;

        public UnitOfWork(DatabaseContext context)
        {

            _context = context;

            Customers = new CustomersRepository(_context);

        }

        public async Task<int> CompleteAsync()
        {

            return await _context.SaveChangesAsync();

        }

        public void Dispose()
        {

            _context.Dispose();

        }

    }

我的客户实体

public class Customer
    {

        public int Id { get; set; }

        public string Firstname { get; set; }

    }

我的测试详细信息页面

@page "/customers/{CustomerId:int}"

@using RazorApp.Core.Entities
@using RazorApp.Core.Interfaces

@inject IUnitOfWork UnitOfWork

<h1>Customer details</h1>

@if (customer == null)
{

    <p>Loading ...</p>

}
else
{

    <div class="card">

        <div class="card-body">

            <div class="card-text">

                <div class="form-group">

                    <label>Id</label>
                    <input type="text" class="form-control" value="@customer.Id" disabled />

                </div>

                <div class="form-group">

                    <label>Firstname</label>
                    <input type="text" class="form-control" value="@customer.Firstname" bind="customer.Firstname" />

                </div>

                <button class="btn btn-primary" onclick="@UpdateCustomer">Save</button>

            </div>

        </div>
    </div>

}

@functions {

    [Parameter] int CustomerId { get; set; } = 0;

    Customer customer = null;

    protected override async Task OnInitAsync()
    {

        await LoadCustomer(CustomerId);

    }

    protected async Task LoadCustomer(int customerId)
    {

        customer = null;
        customer = await UnitOfWork.Customers.GetAsync(customerId);

    }

    protected async void UpdateCustomer()
    {

        UnitOfWork.Customers.Update(customer);
        await UnitOfWork.CompleteAsync();

    }

}

我希望仅当我调用UoW customer.Firstname = "123"函数时,才应保存对对象的更改(使用数据绑定或仅进行常规更改,例如Complete)。我不希望在我的应用程序中更新任何“缓存/临时/引用”对象。

0 个答案:

没有答案