关于EntityFramwork的查询

一、建立数据库和数据表
1.建立数据库(MyDB),并建立文章表(Article)和类型表(Cate)
2.数据表要求如下:
文章表(Article)

列名要求备注
Idint主键,自增
Titlevachar(50)
Authorvachar(50)
CateIdint

类型表(Cate)

列名要求备注
Idint主键,自增
Namevachar(50)
Commenttext

二、创建web项目
1.新建后缀名为.aspx的页面
2.加入Repeater控件和button控件
3.在button控件的点击事件里输入以下内容
4.在Page_Load事件里输入以下内容

//button点击事件
 protected void Button2_Click(object sender, EventArgs e)
        {
            MyDBEntities1 db = new MyDBEntities1();

            //同时加两个表
            Article art = new Article();
            art.Title = txttitle.Text;
            art.Content = txtcontent.Text;
            art.Author = txtauthor.Text;
            //article.Catelogid = 1;
            art.Catelog = new Catelog { Name = txtcatelog.Text, Content = txtcatecontent.Text };

            db.Article.Add(art);
            db.SaveChanges();
            int count = db.SaveChanges();
            if (count >= 0)
            {
                Response.Write("添加成功");
                var result = from article in db.Article
                             join catelog in db.Catelog on article.Catelogid equals catelog.Id
                             select new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };
                this.Repeater1.DataSource = result.ToList();
                this.Repeater1.DataBind();
            }
//Page_Load加载事件
 protected void Page_Load(object sender, EventArgs e)
        {
            MyDBEntities1 db = new MyDBEntities1();

            var result = from article in db.Article
                         join catelog in db.Catelog on article.Catelogid equals catelog.Id
                         select new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };
            this.Repeater1.DataSource = result.ToList();
            this.Repeater1.DataBind();
        }

这样,我们的EntityFramwork查询就完成了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值