EF入門

安裝ef,須要先下載連個連接工具html

mysql:mysql

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


 Connector/Net:數據庫

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


MySQL for Visual Studio:app

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


[System.Web.Http.Description.ResponseType(typeof(products))]//設置返回值類型
spa


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設置自動類註釋和屬性註釋code

tt文件中設置屬性註釋:htm

	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);
                    }
                }
相關文章
相關標籤/搜索