EF入门

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
10月前
|
SQL 存储 开发框架
EF框架如何搭建
EF框架如何搭建
|
10月前
|
SQL 存储 XML
|
SQL 存储 数据处理
5.1EF Core原理
对普通集合使用where等方法查询出来的返回值为IEnumerable类型 但是对DbSet使用用where等方法出查询出来的返回值为IQueryable类型
|
存储 开发框架 关系型数据库
【菜鸟看框架】——浅谈EF框架
【菜鸟看框架】——浅谈EF框架
108 0
【菜鸟看框架】——浅谈EF框架
|
数据库
EF-ModelFirst实现过程
EF-ModelFirst实现过程
EF-ModelFirst实现过程
|
数据库
[原创]EF-DBFirst实现过程
[原创]EF-DBFirst实现过程
[原创]EF-DBFirst实现过程
|
存储 SQL XML
Entity Framework学习笔记——EF简介(一篇文章告诉你什么是EF)
Entity Framework是以ADO.NET为基础,面向数据的“实体框架”。以下简称EF。 它利用了抽象化数据结构的方式,将每个数据库对象都转换成应用程序对象 (entity),数据字段都转换为属性 (property),关系则转换为结合属性 (association),让数据库的 E/R 模型完全的转成对象模型,如此让程序设计师能用最熟悉的编程语言来调用访问。
Entity Framework学习笔记——EF简介(一篇文章告诉你什么是EF)
|
存储 SQL XML
EF Core-2
上篇 概念性讲述CRQS(https://www.cnblogs.com/ccaa/p/12545582.html)
111 0
EF Core-2