zoukankan      html  css  js  c++  java
  • 【.NET-EF】Entity Framework学习笔记2

    学习描述:用EF就像是省略了做实体类和DAL类,感觉是很方便,废话不多说,直接写步骤:

    1.创建EF的edmx文件

    这个其实在笔记1已说过,不过有些细节也要说,所以再说一遍,这里使用的是EF 6.1版本的。

    建了个商品表,创建文件之前,要有一个数据表,我在本地建个xitong数据库,建了个商品表,如下:

    CREATE TABLE [dbo].[xt_product](
        [pid] [int] IDENTITY(1,1) NOT NULL,
        [code] [nvarchar](100) COLLATE Chinese_PRC_CI_AS NULL,
        [title] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,
        [point] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL,
        [description] [text] COLLATE Chinese_PRC_CI_AS NULL,
        [prise] [money] NULL,
        [createTime] [datetime] NULL,
        [state] [bit] NULL,
        [remark] [nvarchar](500) COLLATE Chinese_PRC_CI_AS NULL
    )

    好了,开始建我的edmx文件,我在项目建个文件夹放文件,就叫EntityFramework,再右键 - 添加 - 新建项 - 数据 - ADO.NET实体数据模型(连接数据库步骤省略)

    要记得自己写的2个类名

    • 一个是  将Web.config中的连接设置另存为  的名字,这个是用操作数据库的方法的,写成: xtSQL
    • 一个就是你的数据表名,会变成实体类名,而且能在生成的文件.tt里看得到实体类,写成:   xt_productEF
    2.增删改代码(已测试可以用,建个页面调用就OK了。)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using xtWeb.EntityFramework; //凡事需要先using
    
    namespace xtWeb.Models
    {
        public class product
        {
            xtSQL xtsql = new xtSQL();//操作类
            /// <summary>
            /// 新增(单个)
            /// </summary>
            /// <param name="ef"></param>
            /// <returns></returns>
            public bool add(xt_product ef)
            {
                xtsql.xt_product.Add(ef);   
                xtsql.SaveChanges();//增删改都需要最后加一句SaveChanges()来保存
                return true;
            }
    
            /// <summary>
            /// 修改(单个)
            /// </summary>
            /// <param name="ef"></param>
            /// <returns></returns>
            public bool update(xt_product ef)
            {
                xt_product Old = xtsql.xt_product.FirstOrDefault(p => p.pid == ef.pid);//找到商品
                Old.code = ef.code;
                Old.title = ef.title;
                xtsql.SaveChanges();
                return true;
            }
    
            /// <summary>
            /// 删除(单个)
            /// </summary>
            /// <param name="ef"></param>
            /// <returns></returns>
            public bool delete(xt_product ef)
            {
                xt_product Old = xtsql.xt_product.FirstOrDefault(p => p.pid == ef.pid);//找到商品
                xtsql.xt_product.Remove(Old);
    
                xtsql.SaveChanges();
                return true;
            }
        }
    }
    3.小结:
    • 是不是很简单,3,4句话就把增删改功能写出来了。
    • 基本使用SaveChanges()才会保存操作。
    • 辅助工具观察生成的SQL语句,之前说过,EF是基于ADO.NET对SQL语句操作,所以可以用SQL自带的SQL Server Profiler(在开始 - Microsoft sql server 2005 - 性能工具里),在网上也查到有其他工具可以使用,这个以后在说。
    • 好像EF5.0之前版本的方法有所不同,不知道自己是什么版本的,可以查看项目里的packages.config(生成时会有的)
  • 相关阅读:
    配置PL/SQL Developer连接Oracle数据库
    解决RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large问题
    解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList问题
    解决spring mybatis 整合后mapper接口注入失败
    如何解决Failed to start component [StandardEngine[Catalina].StandardHost[127.0.0.1].StandardContext[]]问题
    五毛的cocos2d-x学习笔记05-场景与场景动画,动作
    五毛的cocos2d-x学习笔记04-触摸点
    五毛的cocos2d-x学习笔记03-控件
    五毛的cocos2d-x学习笔记02-基本项目源码分析
    五毛的cocos2d-x学习笔记01-创建项目
  • 原文地址:https://www.cnblogs.com/laokchen/p/6285707.html
Copyright © 2011-2022 走看看