一个简单的EF Linq MVC Web应用程序

Posted 书香香鳳金閣紫陽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的EF Linq MVC Web应用程序相关的知识,希望对你有一定的参考价值。

LINQ(Language Integrated Query)语言集成查询是一组用于c#Visual Basic语言的扩展。

它允许编写C#或者Visual Basic代码以操作内存数据的方式,查询数据库。

LINQ的读法:lin k 很多人会误读为lin Q

LINQ的全称:Language-Integrated Query

LINQ的关键词:from, select, in, where, group by, orderby, …

 详情请查阅:https://baike.so.com/doc/4973517-5196197.html

话不多说,看操作。

           步骤一:确定你的开发环境:vs2015  sql2008。

           步骤二:凡事开发都离不开数据库的搭建,搭建你的数据库,插入一条数据,查看是否有数据,这我就不废话了。

-- 用户表
drop table [User] 
Create Table [User] 
(
 "Theserialnumber" int  IDENTITY   (1,   1)   NOT   NULL ,  
 "UserID"          varchar(50)  NOT   NULL ,
 "UserName"        varchar(30),
 "UserSet"         char(2),
 "Userphone"       char(11),
 "UserworkID"      varchar(50),
 "UserlevelID"       varchar(20),   
 "UserTypeID"      varchar(50),
 "UserCreationtime" dateTime,
 "UserhobbyID"     varchar(50),
 )
 alter table [User] add constraint pk_name primary key ("Theserialnumber","UserID");
 --用户工作表
 drop table Work
 Create Table Work
 (
   "UserworkID" varchar(50) Primary Key  NOT   NULL,
   "UserworkType" varchar(50),
   "WorkDetailsID"  varchar(50)
 )
 --用户工作详情表
 drop table WorkDetails
 Create Table WorkDetails
 (
  "WorkDetailsID" varchar(50) Primary Key,
  "WorkDetailsSituation" varchar(50)
  
 )
 
 --权限表
 drop table [level]
 Create Table [level]
 (
  "UserlevelID" varchar(50)  Primary Key,
  "UserleverlType" varchar(50)  
 )
 
 --类型表
 drop table UserType
 Create Table UserType
 (
 "UserTypeID" varchar(50)  Primary Key,
 "UserType"   varchar(50)
 )
 
 --业余表
 drop table Userhobby
 Create Table Userhobby
 (
  "UserhobbyID" varchar(50) Primary Key,
  "Userhobby"   varchar(50)
 )
 
--增加数据
 insert into [User](UserID,UserName,UserSet,Userphone,UserworkID,
                     UserlevelID,UserTypeID,UserCreationtime,UserhobbyID)
                     values(\'2020020901494949\',\'张三\',\'\',\'13817475159\',\'1\',
                     \'1\',\'1\',2020/2/9,\'1\')
               
insert into Work(UserworkID, UserworkType, WorkDetailsID)values(\'1\',\'IT\',\'2020020901494949\')


insert into WorkDetails(WorkDetailsID, WorkDetailsSituation)values(\'2020020901494949\',\'开发编码\')

insert into [level] (UserlevelID, UserleverlType)values(\'1\',\'管理员\')
insert into [level] (UserlevelID, UserleverlType)values(\'2\',\'普通用户\')

insert into UserType(UserTypeID, UserType)values(\'1\',\'开发人员\')

insert into Userhobby(UserhobbyID, Userhobby)values(\'1\',\'编码\')
insert into Userhobby(UserhobbyID, Userhobby)values(\'2\',\'\')
insert into Userhobby(UserhobbyID, Userhobby)values(\'3\',\'学习\')
insert into Userhobby(UserhobbyID, Userhobby)values(\'4\',\'工作\')

           步骤三:打开你的Visual Studio,新建---项目---ASP.NET Web应用程序。

           步骤四:建一个类库Model(模型)和一个数据访问层DAL

           步骤五:在Model建一个ADO.NET实体数据模型来自数据库的EF设计器

           步骤六:建立引用(索引)  

                          DAL索引Model和程序集框架System.Data.Linq系统配置文件

                          Web应用程序索引DAL和Model

                          在应用程序和DAL里导入Nu Get程序包:EntityFramwork

                          配置好Web.config文件

            步骤七:在DAL数据访问层中搭建一个类,来编写Linq,你需要的功能操作

        /// <summary>
        /// 多表查询
        /// </summary>
        /// <returns></returns>
        public static IQueryable GetDateSet()
        {
            FamilyServerEntities Family = new FamilyServerEntities();
            var v = from p in Family.User
                    join Work in Family.Work on p.UserworkID equals Work.UserworkID
                    join Level in Family.level on p.UserlevelID equals Level.UserlevelID
                    join Type in Family.UserType on p.UserTypeID equals Type.UserTypeID
                    join Details in Family.WorkDetails on Work.WorkDetailsID equals Details.WorkDetailsID
                    join Userhobby in Family.Userhobby on p.UserhobbyID equals Userhobby.UserhobbyID
                    orderby p.UserID
                    select new
                    {
                        UserID = p.UserID,
                        UserName = p.UserName,
                        UserSet = p.UserSet,
                        Userphone = p.Userphone,
                        UserworkType = Work.UserworkType,
                        WorkDetailsSituation = Details.WorkDetailsSituation,
                        UserleverlType = Level.UserleverlType,
                        UserType = Type.UserType1,
                        UserCreationtime = p.UserCreationtime,
                        Userhobby = Userhobby.Userhobby1
                    };
            // var Lambda = Family.User.Select(t => new { });
            return v;
        }

        /// <summary>
        /// 增加多条
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int ADD(List<User> list)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                foreach (var item in list)
                {
                    Family.User.Add(item);
                }
                return Family.SaveChanges();
            }
        }

        /// <summary>
        /// 修改多条
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Edit(List<User> list)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                var v = (from p in Family.User select p).First();
                foreach (var item in list)
                {
                    v.UserName = item.UserName;
                }
                return Family.SaveChanges();
            }
        }

        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static int Del(string UserID)
        {
            using (FamilyServerEntities Family = new FamilyServerEntities())
            {
                var v = (from p in Family.User where p.UserID == UserID select p).ToList();
                foreach (var item in v)
                {
                    Family.User.Remove(item);
                }
                return Family.SaveChanges();
            }
        }

 

           步骤八:在控制器里访问方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebLinq.Controllers
{
    public class HomePageController : Controller
    {
        // GET: HomePage
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult HomePage()
        {
            return Json(BLL.Family.GetDateSet(), JsonRequestBehavior.AllowGet);
            
        }
    }
}

     

 

           步骤九:展示功能效果

可以在jquery中用Ajax方法,Post请求,GetJson请求访问控制器再遍历到HTML表格或控件中

好了,来总结一下,其实挺简单的。

 

以上是关于一个简单的EF Linq MVC Web应用程序的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET EF(LINQ/Lambda查询)

MVC +EF+linq 多表联查

LINQ 和 EF6:创建模型时无法使用上下文

在不使用 LINQ 和 EF 的情况下从 ASP.NET 切换到 ASP MVC

ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统

ASP.NET MVC: EF 没有创建数据库表