当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net mvc 三层加EF两表联查

asp.net mvc 三层加EF两表联查

2019年03月24日  | 移动技术网IT编程  | 我要评论

吴育臻 ed2k,舞剧《莲》,兽药信息

首先打开vs软件
新建项目
创建web中的mvc项目
再右击解决方案创建类库项目
分别创建dal层和bll层再把dal层和bll层的类重命名
在mvc项目中的models文件夹创建model类
在dal创建ado.net实体数据模型后把dal层中app.config文件中的链接字符串复制到mvc项目的web.config文件中
dal层中的类开始打代码

 /// <summary>
        /// 两表联查
        /// </summary>
        /// <returns></returns>
        public static list<dynamic> biao()
        {
            using (kaoshientities db = new kaoshientities())
            {
                var sql = from s in db.student
                          join c in db.bang on s.id equals c.bid
                          select new
                          {
                              s.name,
                              s.passwork,
                              c.bname
                          };
                list<dynamic> li = new list<dynamic>();
                foreach (var item in sql.tolist())
                {
                    dynamic d = new expandoobject();
                    d.name = item.name;
                    d.pwd = item.passwork;
                    d.bname = item.bname;
                    li.add(d);
                }
                return li;
            }
        }

bll层

 /// <summary>
        /// 两表联查
        /// </summary>
        /// <returns></returns>
        public static list<dynamic> biao()
        {
            try
            {
                return kaoshidal.kaoshidal.biao();
            }
            catch (exception ex)
            {
                
                throw ex;
            }
        }

mvc项目中的models文件夹的model类

 /// <summary>
        /// 两表联查
        /// </summary>
        /// <returns></returns>
        public static list<dynamic> biao()
        {
            try
            {
                return kaoshibll.kaoshibll.biao();
            }
            catch (exception ex)
            {

                throw ex;
            }
        }

在mvc项目中的controllers文件夹创建home控制器

 /// <summary>
        /// 两表联查
        /// </summary>
        /// <returns></returns>
        public actionresult index()
        {
            list<dynamic> li =kaoshimodel.biao();
            return view(li);
        }

index视图

@{
    viewbag.title = "index";
}

<h2>index</h2>
<table style="width: 40%;" border="1">
    <tr>
        <th>姓名</th>
        <th>密码</th>
        <th>班级</th>
    </tr>
    @foreach (var item in model)
    {
    <tr>
        <td>@item.name</td>
        <td>@item.pwd</td>
        <td>@item.bname</td>
    </tr>
      }
</table>

 



                    

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网