0

我目前正在使用 Razor 组件、Entity Framework Core 和一些标准模式(存储库模式、UnitOfWork)构建一个测试应用程序。我的实体中的更改是临时保存的(如果我浏览页面,更改是可见的),而无需调用SaveChanges我的 UoW 类(错误)。刷新应用程序会删除这些临时文件。更改 - 调用我的工作单元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"));

数据库上下文.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);

        }


    }

客户存储库.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我不希望在我的应用程序中更新任何“缓存/临时/引用”对象。

4

0 回答 0