从ADO转换为实体框架 [英] Convert from ADO to entity framework

查看:76
本文介绍了从ADO转换为实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将这段代码更改为实体框架



I need to change this piece of code to entity framework

string getQuerystring = queueListBox.Text.Substring(0, 4).Replace(" ", string.Empty);
queryString = "Select * from reservation where Id= '" + getQuerystring + "'";

query = new SqlCommand(queryString, connection);





我尝试过:



尝试将此更改为实体框架



What I have tried:

try to change this to entity framework

推荐答案

这是EF中的一个非常基本的操作。我强烈建议你拿一本关于EF的书来解决它。



我们无法准确地告诉你代码是什么,因为我们知道什么都没有您的EF数据库设置,就像您的DbContext名称一样。我们不知道你所谓的预订表名称,Id字段是什么,......没什么。



但是,有几种不同的做法这个:

This is a very basic operation in EF. I highly suggest you pick up a book on EF and work through it.

We can't tell you exactly what the code is going to be because we know NOTHING about your EF database setup, like your DbContext name. We don't know what you called your reservation table name, what the Id field is, ... nothing.

But, there's a couple of different ways of doing this:
var result = myDbContext.Reservations.SingleOrDefault(r => r.Id == queryId);






or

var result = myDbContext.Reservations.Where(r => r.Id == queryId).SingleOrDefault();






or

var result = from r in myDbContext.Reservations
             where r.Id == queryId
             select r;


这篇关于从ADO转换为实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆