EF

EF

1.引入框架

  新建一个Web-MVC项目,并在项目中添加ADO.NET 尸体数据模型,命名为study

  

  

  

  新建连接,选择数据库

  

  确定,生成study.edmx

2.语法介绍

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EFdemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            //实例化一个ADO.NET
            studyEntities stuen = new studyEntities();
            //---------------------------------------Linq语法--------------------------------------

            #region 查,其他可以看上一章的知识
            //List<Student> Liststudent = (from s in stuen.Students select s).ToList();
            //foreach (Student s in Liststudent)
            //{
            //    Console.WriteLine(s.ID);
            //    Console.WriteLine(s.StuName);
            //    Console.WriteLine(s.Phone);
            //    Console.WriteLine(s.Address);
            //    Console.WriteLine(s.City);
            //} 
            #endregion

            //---------------------------------------EF语法----------------------------------------
            #region//Student stu = new Student();
            //stu.ID = 108;
            //stu.Phone = "1528809668";
            //stu.StuName = "flt";
            //stu.Address = "吴川";
            //stu.City = "湛江";
            //stuen.Students.Add(stu);//内存上面的操作
            //stuen.SaveChanges();
            #endregion

            #region//表中所有数据
            //List<Student> stus= stuen.Students.ToList();
            //按ID查询数据
            //stuen.Students.Where(p => p.ID == 108);

            #endregion

            #region//Student stu = (stuen.Students.Where(p => p.ID == 108)).FirstOrDefault();
            //stu.City = "茂名";
            //stuen.SaveChanges();
            #endregion

            #region//方法一
            //Student stu = (stuen.Students.Where(p => p.ID == 108)).FirstOrDefault();
            //stuen.Students.Remove(stu);
            //stuen.SaveChanges(); 
            //方法二
            //Student stu = new Student()
            //{
            //    ID = 108
            //};
            //stuen.Students.Attach(stu);//stu为新定义的一个对象,并非集合Students里面的数据,用这个附加语句可以根据参数进行映射
            //stuen.Students.Remove(stu);
            //stuen.SaveChanges();
            #endregion

            return View();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/wskxy/p/9179890.html
EF