C# MVC 3中不使用db上下文的CRUD操作

C# MVC 3中不使用db上下文的CRUD操作,c#,asp.net-mvc,C#,Asp.net Mvc,我需要帮助。实际上我正在使用ADO net和4.0.net framework,并在解决方案资源管理器中使用app_数据创建自己的数据库,扩展名为.mdf using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Crudoperations.Models; using System.Data.Entity; namespace

我需要帮助。实际上我正在使用ADO net和4.0.net framework,并在解决方案资源管理器中使用app_数据创建自己的数据库,扩展名为.mdf

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Crudoperations.Models;
using System.Data.Entity;

namespace Crudoperations.Controllers
{
public class EmployeeController : Controller
{
    //
    // GET: /Employee/

    EmployEntities employContext;


    public ActionResult Index()
    {
        using (var details= new EmployEntities())
        {
            return View(details.employs.ToList());
        }
    }

    //
    // GET: /Employee/Details/5

    public ActionResult Details(int id)
    {

        employContext = new EmployEntities();
        employ _TempEmploy = new employ();
        _TempEmploy.Id = id;


        return View(_TempEmploy);
    }

    //
    // GET: /Employee/Create

    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Employee/Create

    [HttpPost]
    public ActionResult Create(employ objEmploy)
    {
        try
        {
            employContext = new EmployEntities();
            employContext.AddToemploys(objEmploy);
            employContext.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    public ActionResult Edit(int id)
    {
        employContext = new EmployEntities();
        employ _employ = new employ();
        //_employ = ( from Employ
        //            where ID = id) 
        return View();
    }
    [HttpPost]
    public ActionResult Edit(int id, employ objEmploy)
    {
        try
        {

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    //
    // GET: /Employee/Delete/5

    public ActionResult Delete(int id)

    {
        employ _TempEmploy = new employ();


            return View(_TempEmploy);


    }

    //
    // POST: /Employee/Delete/5

    [HttpPost]
    public ActionResult Delete(int id, employ objEmploy)
    {
        try
        {
            //employContext = new EmployEntities();
            //employ _TempEmploy = new employ();
            //_TempEmploy.Id = id;
            //employ _employ = employContext.Find(_TempEmploy);
            employContext.DeleteObject(objEmploy);
            employContext.SaveChanges();
            return RedirectToAction("Index");

        }
        catch
        {
            return RedirectToAction("Delete", new { id = id, saveChangesError = true });

        }
    }

    public IView detailsModel { get; set; }
}
}

事实上,我在使用ID访问数据时遇到了问题,即在db上下文中有一个方法“Find(ID)”,但在对象上下文中没有这种类型的方法。我要做的是访问数据以进行编辑、详细说明并从数据库中删除数据

您可以使用ID获取数据,它将获取第一个匹配的对象

db.Users.FirstOrDefault(x => x.UserId== Id);

希望它能帮助你。祝您度过愉快的一天。

您能添加一些代码示例吗?db也是上下文的一个对象。您可以使用context.Users.FirstOrDefault(x=>x.UserId==Id);你能把一段代码放在你正在处理的地方吗。我很乐意帮你:)嘿,我用query来获取数据,但它不起作用。它显示数据库表不存在的错误