MVC和EF(entityframework)的简单使用

1.首先我们需要在NuGet中下载entityframework的包
MVC和EF(entityframework)的简单使用_第1张图片
就是这个红色的
2.在Model文件夹中创建你需要的类型
grade类型
MVC和EF(entityframework)的简单使用_第2张图片
Student类型
MVC和EF(entityframework)的简单使用_第3张图片
3.在web.conify文件中写入

 <connectionStrings>
    <add connectionString="Data Source=.;Initial Catalog=Department;Integrated Security=True" providerName="System.Data.SqlClient" name="con"/>
  </connectionStrings>

详细位置在这里
MVC和EF(entityframework)的简单使用_第4张图片
4.在Model文件夹中创建一个addcontext名字的类型

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace WebApplication4.Models
{
    public class Addcontext:DbContext
    {
        /// 
        /// 连接数据库
        /// 
        public Addcontext() : base("con")
        {
            Database.SetInitializer<Addcontext>(null);
        }
        /// 
        /// 表和表之间的关系
        /// 
        /// 
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention >();
        }
        //注册类型
        public DbSet<Student> students{get;set;};
        public Dbset<grade> grades{get;set;};
       
    }
}

5.接下来打开你的NuGet控制台
MVC和EF(entityframework)的简单使用_第5张图片
6.输入指令
(1)Enable-Migrations
成功后
参数写
(2)Add-Migration
参数随便写
参数写
成功后
在这里插入图片描述在这里插入图片描述
在这里写你的默认数据
MVC和EF(entityframework)的简单使用_第6张图片
(3)Update-Database
成功
在这里插入图片描述
7.方法接口
在Model文件夹中创建一个Interface的接口,里面是方法
MVC和EF(entityframework)的简单使用_第7张图片
8.写详细的方法
创建一个Setstudent名字的类(注意创建后的第一件事就是实现addcontext的注入)

 protected readonly addcontext context = new addcontext();
        /// 
        /// 添加
        /// 
        /// 
        /// 
        public student Addstudent(student student)
        {
            context.Students.Add(student);
            context.SaveChanges();
            return student;
        }
        /// 
        /// 删除
        /// 
        /// 
        /// 
        public student Delstudent(int id)
        {
            student students = context.Students.Find(id);
            if (students != null)
            {
                context.Students.Remove(students);
                context.SaveChanges();
            }
            return students;
        }
        /// 
        /// 读取对应的
        /// 
        /// 
        /// 
        public student Getstudent(int id)
        {
            return context.Students.Find(id);
        }
        /// 
        /// 读取全部
        /// 
        /// 
        public IEnumerable<student> GetStudents()
        {
            return context.Students;
        }
        /// 
        /// 修改
        /// 
        /// 
        /// 
        public student upstudent(student upstudent)
        {
            context.Students.Add(upstudent);
            context.Entry(upstudent).State = System.Data.Entity.EntityState.Modified;
            context.SaveChanges();
            return upstudent;
        }

9.控制器里面使用
注入接口
MVC和EF(entityframework)的简单使用_第8张图片

你可能感兴趣的:(MVC和EF(entityframework)的简单使用)