关于开发时使用的C#EF5模型随笔

using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Archives.UI.Data;
using Newtonsoft.Json;

namespace Archives.UI.Controllers
{
public class PunishmentAwardsController : Controller
{
// GET: PunishmentAwards
public ActionResult Index()
{
return View();
}

    /// <summary>
    /// EF模型对象
    /// </summary>
    StudentArchivesEntities db = new StudentArchivesEntities();

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    /*
            前端测试用例
            url地址:http://localhost:50569/PunishmentAwards/select
            提交方法:get
            参数:不含参
            回调:Json数组[{"PunishAwardsRecodeID":1,"PunishAwardTypeID":1,"StudentID":"201717380101","PunishAwardContent":"全勤","PunishAwardReason":"全勤","PunishAwardDate":"02 25 2019 12:00AM"},{"PunishAwardsRecodeID":2,"PunishAwardTypeID":1,"StudentID":"201717380101","PunishAwardContent":"全勤","PunishAwardReason":"全勤","PunishAwardDate":"02 25 2019 12:00AM"}]
     */
    [HttpGet]
    public string Select()
    {
        ///将EF模型中PunishmentAwardsRecode表的所有数据提取出来转为List<T>类型
        List<PunishmentAwardsRecode> list = db.PunishmentAwardsRecode.ToList();

        ///将List<T>对象转为json字符串
        JavaScriptSerializer js = new JavaScriptSerializer();

        ///返回相应数据到前端
        return js.Serialize(list);
    }


    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="punishmentAwardsRecode"></param>
    /// <returns></returns>
    /*
             前端测试用例
             url地址:http://localhost:50569/PunishmentAwards/add
             提交方法:post
             参数提交方式:formurlencode
             参数使用键值对提交(name:value):例如
                result:{"PunishAwardsRecodeID":1,"PunishAwardTypeID":1,"StudentID":"201717380101","PunishAwardContent":"全勤","PunishAwardReason":"全勤","PunishAwardDate":"02 25 2019 12:00AM"}
                注:只提交一个name为result参数,value值为需要添加的信息转化为json字符串格式
                参数值:PunishAwardsRecodeID可为任意值
             回调:成功执行返回true,若不成功则返回错误信息
    */
    [HttpPost]
    public string Add(string  result )
    {
        try
        {
            ///将前端传的string类型数据转为PunishmentAwardsRecode对象
            PunishmentAwardsRecode punishmentAwardsRecode = JsonConvert.DeserializeObject<PunishmentAwardsRecode>(result);

            ///将PunishmentAwardsRecode对象添加到EF模型
            db.PunishmentAwardsRecode.Add(punishmentAwardsRecode);

            ///保存结果
            db.SaveChanges();
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message;
            throw;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="PunishAwardsRecodeID">PunishAwardsRecodeID</param>
    /// <returns></returns>
    /*
           前端测试用例
            url地址:http://localhost:50569/PunishmentAwards/del
            提交方法:get
            参数:name:value 
            参数提交方法:params
            回调:成功执行返回true,若不成功则返回错误信息
     */
    [HttpGet]
    public string Del(string PunishAwardsRecodeID)
    {
        try
        {
            ///根据PunishAwardsRecodeID查询到原始数据
            PunishmentAwardsRecode punishmentAwardsRecode = db.PunishmentAwardsRecode.Find(int.Parse(PunishAwardsRecodeID));

            ///删除原始数据
            db.PunishmentAwardsRecode.Remove(punishmentAwardsRecode);

            ///保存结果
            db.SaveChanges();
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message;
            throw;
        }
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="result"></param>
    /// <returns></returns>
    /*
             前端测试用例
             url地址:http://localhost:50569/PunishmentAwards/add
             提交方法:post
             参数提交方式:formurlencode
             参数使用键值对提交(name:value):例如
                result:{"PunishAwardsRecodeID":1,"PunishAwardTypeID":1,"StudentID":"201717380101","PunishAwardContent":"全勤","PunishAwardReason":"全勤","PunishAwardDate":"02 25 2019 12:00AM"}
                注:只提交一个name为result参数,value值为需要添加的信息转化为json字符串格式
             回调:成功执行返回true,若不成功则返回错误信息
    */
    [HttpPost]
    public string Updata(string result)
    {
        try
        {
            ///将前端传的字符串转为PunishmentAwardsRecode对象
            PunishmentAwardsRecode punishmentAwardsRecode = JsonConvert.DeserializeObject<PunishmentAwardsRecode>(result);

            ///根据PunishAwardsRecodeID找到原数据
            PunishmentAwardsRecode NewpunishmentAwardsRecode = db.PunishmentAwardsRecode.Find(punishmentAwardsRecode.PunishAwardsRecodeID);

            ///将原数据添加到DbEntityEntry对象
            DbEntityEntry<PunishmentAwardsRecode> dbEntityEntry = db.Entry<PunishmentAwardsRecode>(NewpunishmentAwardsRecode);

            ///设置dbEntityEntry对象为修改状态才能修改
            dbEntityEntry.State = System.Data.EntityState.Unchanged;

            ///设置哪些值可以进行改变
            dbEntityEntry.Property(p => p.PunishAwardTypeID).IsModified = true;
            dbEntityEntry.Property(p => p.PunishAwardReason).IsModified = true;
            dbEntityEntry.Property(p => p.PunishAwardContent).IsModified = true;
            dbEntityEntry.Property(p => p.PunishAwardDate).IsModified = true;

            ///修改原始值
            NewpunishmentAwardsRecode.PunishAwardTypeID = punishmentAwardsRecode.PunishAwardTypeID;
            NewpunishmentAwardsRecode.PunishAwardReason = punishmentAwardsRecode.PunishAwardReason;
            NewpunishmentAwardsRecode.PunishAwardContent =punishmentAwardsRecode.PunishAwardContent;
            NewpunishmentAwardsRecode.PunishAwardDate =punishmentAwardsRecode.PunishAwardDate;

            ///保存
            db.SaveChanges();
            return "true";
        }
        catch (Exception ex)
        {
            return ex.Message;
            throw;
        }
    }

    /// <summary>
    /// 奖惩类别查询
    /// </summary>
    /// <returns></returns>
    /*
            前端测试用例
            url地址:http://localhost:50569/PunishmentAwards/SelectTypes
            提交方法:get
            参数:不含参
            回调:Json数组[{"PunishAwardsTypeID":1,"PunishAwardsTypeName":"全勤","PunishAwardsName":"1"}]
     */
    [HttpGet]
    public string SelectTypes()
    {
        List< PunishmentAwardsTypes> punishmentAwardsTypes = db.PunishmentAwardsTypes.ToList();
        return JsonConvert.SerializeObject(punishmentAwardsTypes);
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值