I am aware of the possibility to map a function to a static method on the DbContext in Entity Framework Core, but i am trying to avoid static methods, such that the method can be declared in an interface, which is then used throughout the application instead of the concrete DbContext implementation. Is this possible?
Something like this:
public class Entity
{
int Id { get; set; }
}
public interface IApplicationDbContext {
IQueryable<Entity> Entities { get; }
int GetSomeInfo(int id);
}
public class ApplicationDbContext : DbContext, IApplicationDbContext {
public DbSet<Entity> EntitySet { get; set; }
public IQueryable<Entity> Entities => EntitySet;
[DbFunction("GetSomeValue", "dbo")]
public int GetSomeValue(int id)
{
throw new NotImplementedException();
}
}
...
public void SomeMethod(IApplicationDbContext context)
{
int value = 5;
IList<Entity> entities = context.Entities
.Where(x => context.GetSomeValue(x.Id) == value)
.ToList();
}
...
question from:
https://stackoverflow.com/questions/65830086/in-ef-core-can-i-map-a-scalar-function-to-a-dbcontext-instance-method 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…