EF入门

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: EF入门

安装ef,需要先下载连个链接工具


mysql:


http://dev.mysql.com/downloads/windows/installer/


Connector/Net:


http://dev.mysql.com/downloads/connector/net/


MySQL for Visual Studio:


http://dev.mysql.com/downloads/windows/visualstudio/


[System.Web.Http.Description.ResponseType(typeof(products))]//设置返回值类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFTest
{
    using System.Data.Entity;
    using System.Runtime.CompilerServices;
    using EFTest.Models;
    class Program
    {
        private static readonly laojiahenanContext dbContext = new laojiahenanContext();
        static void Main(string[] args)
        {
            //var userList = dbContext.yf_user.ToList();
            //foreach (var user in userList)
            //{
            //    System.Console.WriteLine(user.userPhone);
            //}
            //var addUser = dbContext.yf_user.FirstOrDefault(u => u.userPhone == "15286819573");
            dbContext.Database.Log = Console.WriteLine;
            var query = (from user in dbContext.yf_user//.Where(u=>u.userPhone=="15286819572")
                         join community in dbContext.yf_community on user.communityId equals community.id
                         orderby user.userId descending 
                        select new { user, community }).Skip(1).Take(2);
            var list = query.ToList();
            foreach (var q in query)
            {
                string communityName =string.Empty;
                string userPhone = string.Empty;
                if (q.community != null)
                {
                    communityName = q.community.communityName;
                }
                if (q.user != null)
                {
                    userPhone = q.user.userPhone;
                }
                System.Console.WriteLine("{0}---{1}", communityName, userPhone);
            }
            //if (addUser == null)
            //{
            //    dbContext.yf_user.Add(new yf_user() { userPhone = "15286819573", userPass = "123456" });
            //    dbContext.SaveChanges();
            //}
            //else
            //{
            //    //addUser.userName = "杨帆";
            //    //System.Console.WriteLine(addUser.userName);
            //    dbContext.yf_user.Remove(addUser);
            //    dbContext.SaveChanges();
            //}
            Console.ReadLine();
        }
    }
}
左关联
 var query = (from user in dbContext.yf_user.Where(u=>u.userId==UserId)
                         join com2 in dbContext.yf_community
                         on user.communityId equals com2.id into JoinedEmpDept
                         from com in JoinedEmpDept.DefaultIfEmpty()
                         select new
                         {
                             user.userId,
                             user.userName,
                             user.userPhoto,
                             user.sex,
                             user.userPhone,
                             user.communityId,
                             user.createTime,
                             user.note,
                             user.isEnable,
                             com.communityName
                         }).FirstOrDefault();

Mode1.demx设置自动类注释和属性注释

tt文件中设置属性注释:

  string summary=string.Empty;
    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
          if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null)
          {
             summary=edmProperty.Documentation.Summary;
          }
          else
          {
            summary="";
          }
#>
    /// <summary>
    /// <#=summary#>
    /// </summary>
    <#=codeStringGenerator.Property(edmProperty)#>
<#
        }
    }

遍历input中不为空的字段,修改到oldData中,input和oldData必须为同一种Class

//遍历入参的类.找出修改了哪些类型
                foreach (PropertyInfo p in input.GetType().GetProperties())
                {
                    //value不为空
                    if (p.GetValue(input) != null)
                    {
                        //在数据库中找出的数据类中查找并setValue该值
                        PropertyInfo propertyName = typeof(products).GetProperty(p.Name);
                        propertyName.SetValue(oldData, p.GetValue(input), null);
                    }
                }

EF扩展库

1.EntityFramework-Plus   http://entityframework-plus.net/

2.EntityFramework.Utilities  http://www.jianshu.com/p/dff3c684a0e4


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
安全
ES安全完整的重启步骤
ES安全完整的重启步骤
438 0
|
存储 缓存 安全
《java核心技术36讲》学习笔记-------杨晓峰(极客时间)
《java核心技术36讲》学习笔记-------杨晓峰(极客时间)
411 0
|
存储 SQL NoSQL
非关系型数据库(NoSQL)
【4月更文挑战第9天】非关系型数据库(NoSQL)是分布式、非结构化、不遵循ACID原则的数据存储系统,常用于大规模数据存储和高并发场景。其类型包括键值对、文档型、列式和图形数据库。与关系型数据库不同,NoSQL数据模型更灵活,适合社交媒体和物联网应用,但在复杂事务处理上不如后者。实际应用中,通常会根据需求选择合适的数据库类型。
433 3
|
JavaScript 前端开发 API
Vue的优缺,vue3的优缺点
Vue的优缺,vue3的优缺点
291 0
|
监控 负载均衡 应用服务中间件
Keepalive 解决nginx 的高可用问题
Keepalive 解决nginx 的高可用问题
|
小程序 搜索推荐 JavaScript
美食推荐|美食推荐小程序|基于微信小程序的美食推荐系统设计与实现(源码+数据库+文档)
美食推荐|美食推荐小程序|基于微信小程序的美食推荐系统设计与实现(源码+数据库+文档)
397 2
|
存储 Linux Shell
Linux内核追踪(一):proc/sys/debugfs
Linux内核追踪(一):proc/sys/debugfs
380 0
|
Cloud Native Java 应用服务中间件
Sentinel 1.8.0 年度版本发布,熔断降级重构升级
在经过数月的打磨后,Sentinel 1.8.0 版本正式发布!该版本是本年度最重要的版本之一,包含大量特性改进与 bug 修复,尤其是针对熔断降级特性的完善升级(支持任意统计时长、慢调用比例降级策略、熔断器事件监听)。
3002 82
Sentinel 1.8.0 年度版本发布,熔断降级重构升级
|
移动开发 小程序 API
开源即时通讯IM框架 MobileIMSDK v6.4 发布
本次更新为次要版本更新,进行了若干优化(更新历史详见:码云 Release Notes、Github Release Notes)。MobileIMSDK 可能是市面上唯一同时支持 UDP+TCP+WebSocket 三种协议的同类开源IM框架。
337 0
|
架构师 机器人 Java
测试理论-软件测试理论基础
软件测试的IEEE定义:使用人工或自动的手段来运行或测量软件系统的过程,目的是检验软件系统是否满足规定的需求,并找出与预期结果之间的差异。
380 2
测试理论-软件测试理论基础