安装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