我使用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