@maliming In ABP.IO we are creating an entities like this which supports repository pattern.
public class Book : Entity<long> { public string Name { get; set; }
public float Price { get; set; }
}
private readonly IRepository<Book,long> _bookRepository;
but i want to create a class which will not inherit Entity.
public class Book { public long Id {get;set;}
public string Name { get; set; }
public float Price { get; set; }
}
private readonly IRepository<Book,long> _bookRepository it gives erros at this line if we didnt pass Entity.
can i use this class with Repository pattern and if not then what approach should i take.
waiting for your urgent response.
2 Answer(s)
-
0
Hi suraj.kumbhar, Due to the generic constraints of IRepository, the entity class must inherit from the
IEntity
/IEntity<TKey>
interface. If you need to create an entity class that does not inherit fromIEntity
, you can only access entity data throughDbContext
. e.g.public class CustomRepository : ICustomRepository { private readonly IDbContextProvider<IoTProjectDbContext> _dbContextProvider; protected virtual Task<IoTProjectDbContext> GetDbContextAsync() { return _dbContextProvider.GetDbContextAsync(); } public CustomRepository(IDbContextProvider<IoTProjectDbContext> dbContextProvider) { _dbContextProvider = dbContextProvider; } public async Task<DeviceTests> GetAsync(long id) { var provider = await GetDbContextAsync(); return await provider.DeviceTests.FirstOrDefaultAsync(e => e.Id == id); } }
-
0
This question has been automatically marked as stale because it has not had recent activity.