简体   繁体   中英

How to use Find(params object[] keyValues) method on a DbSet instance in ASP.NET Core?

I am currently migrating an ASP.NET webform into ASP.NET Core. I have a small issue with a specific Find method. I am quite new to .NET so I hope that the code would explain it better.

var env = Request.Headers.GetValues("environment").First();
var db = new FormsContext(env);
var request = db.InfoRequests.Find(Convert.ToInt32(id));

I am specifically trying to find a replacement for Find(Convert.ToInt32(id)) ; part of the code where "id" comes in as a string to the method.

To be more specific, in .NET Core they removed System.Data.Entity and this find method used to come from there. (This is from ASP.NET, not .NET Core)

System.Data.Entity.DbSet<InfoRequest>.Find(params object[] keyValues);

however the DbSet method that I am using now is from following: (This one is from .NET Core)

Microsoft.EntityFrameworkCore.DbSet<InfoRequest>

but this class does not have a find method that I can use. Or I am unaware of the syntax.

Lastly, a small snippet from how "InfoRequest" class is created:

public partial class FormsContext : DbContext
{
    ...
    public virtual DbSet<InfoRequest> InfoRequests { get; set; }
    ...
}

I hope I was able to provide enough information, but shortly, I am trying to find a replacement in .NET Core for the "find" method. Or if you can replace the three lines that I have first shared with an alternative, that should suffice as well. Thanks.

AFAIK, The Find method is not part of EF 1.0 however it is marked that it will be part of EF 1.1 as the EF GitHub issue shows. You can find sample implemented extension method here

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