简体   繁体   中英

How to Update Entity without a DbContext in class library

How can I update an entity in a repository without a concrete DbContext? I am trying to keep the library completely decoupled from the DbContext implementation by declaring an interface as follows:

public interface ISettingsManagerDbContext
{
    IDbSet<ApplicationSetting> ApplicationSettings { get; set; }

    int SaveChanges();
    Task<int> SaveChangesAsync();
    DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity);
}

I keep getting following error:

The type 'TEntity' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.Infrastructure.DbEntityEntry<TEntity>'

Any suggestions on how I can implement the update method without having the actual DbContext?

Based on that error, I think it means you need to do:

DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity)
   where TEntity: class;

to resolve the constraint. Basically the where defines the constraint that TEntity has to be a class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM