EF框架

Linq to EF

添加:

     
//用户注册
int IUserDAO.Register(Users user) { int uid = 0; using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { EF.DD_Users entity = new EF.DD_Users() { UserName = user.UserName, Password = user.Password, RegistTime = user.RegistTime, CartLevel = user.CartLevel, GradeID = user.GradeID, GoldBalance = user.GoldBalance, NickName = user.NickName, State=user.State, UserNameType=user.UserNameType }; context.DD_Users.Add(entity); context.SaveChanges(); uid = entity.UID; } return uid; }

 

 

 

修改:

//用户修改  
bool IUserDAO.EditUser(Users u) { bool flag = false; using( EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var user = context.DD_Users.SingleOrDefault(a =>a.UID == u.UID); if (user != null) { user.Birthday = u.Birthday; user.Gender = u.Gender; user.IDNumber = u.IDNumber; user.NickName = u.NickName; user.PersonalIDType = u.PersonalIDType; user.Industry = u.Industry; user.Professional = u.Professional; user.RealName = u.RealName; user.AddressID = u.AddressID; user.Address = u.Address; flag=context.SaveChanges()>0?true:false; } } return flag; }

 用户查询:

//用户登录  
      Users IUserDAO.Login(string loginid, string password)
        {
            Users user = null;
            using (EF.ddgwDBEntities context = new EF.ddgwDBEntities())
            {
                var entity = context.DD_Users.FirstOrDefault(u => u.UserName == loginid && u.Password == password);
                if (entity != null)
                {
                    user = CreatUser(user, entity);
                }
            }
            return user;
        }


 private static Users CreatUser(Users user, EF.DD_Users entity)
        {
            user = new Users()
            {
                UserName = entity.UserName,
                UID = entity.UID,
                Birthday = entity.Birthday,
                GradeID = entity.GradeID,
                Email = entity.Email,
                CartLevel = entity.CartLevel,
                GoldBalance = entity.GoldBalance,
                IDBackImage = entity.IDBackImage,
                IDFrontImage = entity.IDFrontImage,
                IDNumber = entity.IDNumber,
                LastAddressID = entity.LastAddressID,
                LastInvoiceTypeID = entity.LastInvoiceTypeID,
                PersonalIDType = entity.PersonalIDType,
                LastLoginTime = entity.LastLoginTime,
                Mobile = entity.Mobile,
                NickName = entity.NickName,
                Password = entity.Password,
                RealName = entity.RealName,
                RegistTime = entity.RegistTime,
                State = entity.State,
                UserNameType=entity.UserNameType,
                Gender = entity.Gender,
                Address = entity.Address,
                Industry = entity.Industry,
                Professional = entity.Professional,
                IsVolunteer=entity.IsVolunteer,
                HeadImage=entity.HeadImage,
                AddressID=entity.AddressID
            };
            return user;
        }

 

删除:

using (EF.ddgwDBEntities context = new EF.ddgwDBEntities())
            {
                var user= context.Users.Where(c => c.Uid== 1).First();
                context.Users.Remove(user);
                context.SaveChanges();
            }

 

 

查询返回集合

 List<InspectionUsers> IProductQualityDAO.GetAllInspection()
        {
            List<VIEWMODEL.InspectionUsers> inspectionUsers = null;
            using (EF.ddgwDBEntities context = new EF.ddgwDBEntities())
            {

                inspectionUsers = (from f in context.DD_InspectionUsers
                                   select new VIEWMODEL.InspectionUsers
                                   {
                                       INSID = f.INSID,
                                       UserName = f.UserName,
                                       Password = f.Password,
                                       Mobile = f.Mobile,
                                       Email = f.Email,
                                       Telephone = f.Telephone,
                                       RealName = f.RealName,
                                       Birthday = f.Birthday,
                                       PersonalIDType = f.PersonalIDType,
                                       IDNumber = f.IDNumber,
                                       IDFrontImage = f.IDFrontImage,
                                       IDBackImage = f.IDBackImage,
                                       CredentialTypeName = f.CredentialTypeName,
                                       CredentialNumber = f.CredentialNumber,
                                       CredentialFrontImage = f.CredentialFrontImage,
                                       CredentialBackImage = f.CredentialBackImage,
                                       Grade = f.Grade,
                                       RegistTime = f.RegistTime,
                                       LastLoginTime = f.LastLoginTime,
                                       LastInspectTime = f.LastInspectTime,
                                       Professional = f.Professional,
                                       INDIDS = f.INDIDS
                                   }).ToList();
            }
            return inspectionUsers;
        }

EF多表联查

 List<ProductInspections> IProductQualityDAO.GetProductsByINSID(int INSID)
        {

            List<ProductInspections> list =new List<ProductInspections>(); 
            using (EF.ddgwDBEntities context = new EF.ddgwDBEntities())
            {
                list = (from pi in context.DD_ProductInspections
                       join p in context.DD_Products on pi.ProductID equals p.PID
                       join psp in context.DD_ProductStylePrices on pi.ProductID equals psp.PID
                       where pi.InspectorID == INSID
                        select new ProductInspections
                       {
                           ID=pi.ID,
                           InspectImgUrl=pi.InspectImgUrl,
                           InspectorID = pi.InspectorID,
                           InspectTime=pi.InspectTime,
                           ProductID=pi.ProductID,
                           Report=pi.Report,
                           ReportingTime=pi.ReportingTime,
                           Score=pi.Score,
                           ProductPrice=psp.Price,
                           ProductImageUrl=pi.InspectImgUrl,
                           ProductName = p.ProductName,
                           PRID = psp.PRID 
                       }).ToList();                     
            }
            return list;
        }

 EF的二次查询(自己胡乱叫的名字)

 IList<SubOrders> IOrderDAO.GetSubOrders(int uid)
        {
            List<SubOrders> list = new List<SubOrders>();
            using (EF.ddgwDBEntities content = new EF.ddgwDBEntities())
            {
                var entities = content.DD_MainOrders.Where(o => o.UserID == uid);
                foreach (var e in entities)
                {
                    var collection = content.DD_SubOrders.Where(o => o.MOID == e.MOID).ToList();
                    foreach (var l in collection)
                    {
                        SubOrders s = new SubOrders();
                        s.MOID = l.MOID;
                        s.PID = l.PID;
                        s.PRID = l.PRID;
                        s.SenderID = l.SenderID;
                        s.SendTime = l.SendTime;
                        s.ShopsID = l.ShopsID;
                        s.SOID = l.SOID;
                        s.SubCoins = l.SubCoins;
                        s.Subtotal = l.Subtotal;
                        s.HasSent = l.HasSent;
                        s.HasShowed = l.HasShowed;
                        s.BuyingTime = l.BuyingTime;
                        s.DeliveryCost = l.DeliveryCost;
                        s.DeliveryMethod = l.DeliveryMethod;
                        s.HasApplyedReparing = l.HasApplyedReparing;
                        s.HasApplyedReplacing = l.HasApplyedReplacing;
                        s.HasApplyedReturning = l.HasApplyedReturning;
                        s.HasComment = l.HasComment;
                        s.HasReceived = l.HasReceived;
                        s.IsFreeCost = l.IsFreeCost;
                        s.Quantity = l.Quantity;
                        s.ReceiveTime = l.ReceiveTime;
                        s.UnitPrice = l.UnitPrice;
                        list.Add(s);
                    }
                }
            }
//查询完后还可继续联查
using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { foreach (var o in list) { EF.DD_ProductStylePrices productStylePrices = context.DD_ProductStylePrices.Single(a => a.PRID == o.PRID); o.ImgUrl = context.DD_ProductImages.Where(p => p.PID == o.PID && productStylePrices.StyleJson.Contains("," + p.pGroupNo + ",")).ToList()[0].Path; o.ProductName = context.DD_Products.FirstOrDefault(p => p.PID == o.PID).ProductName; } } return list; }

 

posted @ 2015-09-10 15:34  .追风逐月  阅读(378)  评论(0)    收藏  举报