欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Entity Framework之DB First方式详解

程序员文章站 2022-05-26 09:42:36
ef(entity framework的简称,下同)有三种方式,分别是:database first、 model first和code first。 下面是db fir...

ef(entity framework的简称,下同)有三种方式,分别是:database first、 model first和code first。

下面是db first的方式:

1. 数据库库中存在两个表,一个是专业表,一个学生表,一个学生只能属于一个专业:

Entity Framework之DB First方式详解

其中t_major是专业表,t_student是学生表,studentid是学号,majorid是专业id,t_major与t_student是一对多的关系。

2. 项目中添加数据库实体模型

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

因为之前没有配置过数据库连接,所以点击“新建库连接”,如果之前配置过数据库连接,可以直接从下拉列表中选择或者新建

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

选择需要生成的表/存储过程等

Entity Framework之DB First方式详解

点击“完成”

Entity Framework之DB First方式详解

这里会弹出如下图的窗口,然后选择确定(如果再弹出,也选择确定),如果不小心点击了取消,可以在模型设计界面ctrl + s(保存的快捷键),或如下图的操作,然后会弹出窗口,一直确定就行。

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

这里是使用mvc,所以添加一个控制器来测试(这里为了快速生成读写的控制器方法,选择“包含读/写操作的mvc5控制器”)

Entity Framework之DB First方式详解

Entity Framework之DB First方式详解

生成代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;

namespace zhong.web.controllers
{
  public class studentcontroller : controller
  {
    // get: student
    public actionresult index()
    {
      return view();
    }

    // get: student/details/5
    public actionresult details(int id)
    {
      return view();
    }

    // get: student/create
    public actionresult create()
    {
      return view();
    }

    // post: student/create
    [httppost]
    public actionresult create(formcollection collection)
    {
      try
      {
        // todo: add insert logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }

    // get: student/edit/5
    public actionresult edit(int id)
    {
      return view();
    }

    // post: student/edit/5
    [httppost]
    public actionresult edit(int id, formcollection collection)
    {
      try
      {
        // todo: add update logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }

    // get: student/delete/5
    public actionresult delete(int id)
    {
      return view();
    }

    // post: student/delete/5
    [httppost]
    public actionresult delete(int id, formcollection collection)
    {
      try
      {
        // todo: add delete logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }
  }
}

同样的方法添加一个major控制器

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;

namespace zhong.web.controllers
{
  public class majorcontroller : controller
  {
    // get: major
    public actionresult index()
    {
      return view();
    }

    // get: major/details/5
    public actionresult details(int id)
    {
      return view();
    }

    // get: major/create
    public actionresult create()
    {
      return view();
    }

    // post: major/create
    [httppost]
    public actionresult create(formcollection collection)
    {
      try
      {
        // todo: add insert logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }

    // get: major/edit/5
    public actionresult edit(int id)
    {
      return view();
    }

    // post: major/edit/5
    [httppost]
    public actionresult edit(int id, formcollection collection)
    {
      try
      {
        // todo: add update logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }

    // get: major/delete/5
    public actionresult delete(int id)
    {
      return view();
    }

    // post: major/delete/5
    [httppost]
    public actionresult delete(int id, formcollection collection)
    {
      try
      {
        // todo: add delete logic here

        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }
  }
}

由于学生表majorid依赖于major表,所以需要先有专业,才能新增学生数据(这里不讨论是否合理)

编写逻辑代码,创建视图

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using zhong.web.models;

namespace zhong.web.controllers
{
  public class majorcontroller : controller
  {
    // get: major
    public actionresult index()
    {
      var majors = new efdbentities().t_major.tolist();
      return view(majors);
    }

    // get: major/details/5
    public actionresult details(int id)
    {
      var major = new efdbentities().t_major.find(id);
      if (major == null)
      {
        return content("参数错误");
      }
      return view(major);
    }

    // get: major/create
    public actionresult create()
    {
      return view();
    }

    // post: major/create
    [httppost]
    public actionresult create(t_major entity)
    {
      if (entity != null)
      {
        var entities = new efdbentities();
        entities.t_major.add(entity);
        entities.savechanges();
      }
      return redirecttoaction("index");
    }

    // get: major/edit/5
    public actionresult edit(int id)
    {
      var entity = new efdbentities().t_major.find(id);
      if (entity == null)
      {
        return content("参数错误");
      }
      return view(entity);
    }

    // post: major/edit/5
    [httppost]
    public actionresult edit(t_major entity)
    {
      if (entity == null)
      {
        return content("参数错误");
      }
      var entities = new efdbentities();
      #region 方式一 
      ////该方式一般是根据主键先读取数据,然后再逐个赋值,最后更新
      //var oldentity = entities.t_major.find(entity.id);
      //if (oldentity!=null)
      //{
      //  oldentity.name = entity.name;
      //  entities.savechanges();
      //}
      #endregion

      #region 方式二
      //该方式是直接将新的实体(可能是new出来的并且对主键等的属性赋值好了)附加到上下文,然后标记状态为修改modified
      entities.t_major.attach(entity);
      entities.entry(entity).state = system.data.entity.entitystate.modified;
      entities.savechanges();
      #endregion
      return redirecttoaction("index");
    }

    // get: major/delete/5
    public actionresult delete(int id)
    {
      var major = new efdbentities().t_major.find(id);
      return view(major);
    }

    // post: major/delete/5
    [httppost]
    public actionresult delete(int id, formcollection collection)
    {
      try
      {
        // todo: add delete logic here
        var entities = new efdbentities();
        var major = entities.t_major.find(id);
        entities.t_major.remove(major);
        entities.savechanges();
        return redirecttoaction("index");
      }
      catch
      {
        return view();
      }
    }
  }
}

添加专业:

Entity Framework之DB First方式详解

专业列表:

Entity Framework之DB First方式详解

同样实现学生控制器与视图:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using zhong.web.models;

namespace zhong.web.controllers
{
  public class studentcontroller : controller
  {
    private efdbentities entities = new efdbentities();
    // get: student
    public actionresult index()
    {
      var students = entities.t_student.tolist();
      return view(students);
    }

    // get: student/details/5
    public actionresult details(int id)
    {
      var student = entities.t_student.find(id);
      return view(student);
    }

    // get: student/create
    public actionresult create()
    {
      viewdata["majorid"] = entities.t_major.select(m => new selectlistitem { text = m.name, value = m.id.tostring() });
      return view();
    }

    // post: student/create
    [httppost]
    public actionresult create(t_student entity)
    {
      entities.t_student.add(entity);
      entities.savechanges();
      return redirecttoaction("index");
    }

    // get: student/edit/5
    public actionresult edit(int id)
    {
      var student = entities.t_student.find(id);
      viewdata["majorid"] = entities.t_major.select(m => new selectlistitem { text = m.name, value = m.id.tostring() });
      return view(student);
    }

    // post: student/edit/5
    [httppost]
    public actionresult edit(t_student entity)
    {
      if (entity == null)
      {
        return content("参数错误");
      }
      entities.t_student.attach(entity);
      entities.entry(entity).state = system.data.entity.entitystate.modified;
      entities.savechanges();
      return redirecttoaction("index");
    }

    // get: student/delete/5
    public actionresult delete(int id)
    {
      var student = entities.t_student.find(id);
      return view(student);
    }

    // post: student/delete/5
    [httppost]
    public actionresult delete(int id, formcollection collection)
    {
      var student = entities.t_student.find(id);
      entities.t_student.remove(student);
      entities.savechanges();
      return redirecttoaction("index");
    }
  }
}

添加学生时,报错如下:

Entity Framework之DB First方式详解

于是在控制器中增加如下代码:

Entity Framework之DB First方式详解

刷新页面:

Entity Framework之DB First方式详解

编辑:

Entity Framework之DB First方式详解

删除:

Entity Framework之DB First方式详解

列表:

Entity Framework之DB First方式详解

在majorcontroller中有介绍ef的两种更新方式。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。