我使用ASP.NET MVC 5創建了一個MVC應用程序,它使用實體框架來處理數據庫。.NET - Microsoft.Data.Entity中是否有DbSet Find方法?
這是一個具有CRUD功能的簡單應用程序,用於將數據項添加到列表中。我可以創建項目並將它們保存到數據庫,現在我想查看特定項目的頁面。
我在之前的應用程序中使用Find
方法在DbSet
對象上完成了此操作,但該方法是System.Data.Entity
的一部分。我能夠創建一個應用程序這種方式,而是用ASP.NET 5,它看起來像我需要以不同的配置數據庫連接,使用這些線Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddMvc();
}
這需要我用,而不是Microsoft.Data.Entity
System.Data.Entity
。
這裏是MyDbContext
類:
public class MyDbContext : DbContext
{
public virtual DbSet<TestModel> TestModels { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=EmptyMVCAuthentication01;integrated security=True;");
}
}
我想要的操作來查看特定頁面,做這樣的事情:
public IActionResult Details(int id)
{
Course course= db.Courses.Find(id);
return View(course);
}
(db
以上是MyDbContext
一個私有的實例)
但是,Microsoft.Data.Entity
中沒有Find
方法。關於我可以從哪裏出發的任何建議?
這裏是一些更多關於此:http://stackoverflow.com/questions/29030472/dbset-doesnt-have-a-find-method-in-ef7 – KrishnaDhungana