Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

c# - In EF Core, can i map a scalar function to a DbContext instance method?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...