将原始SQL查询转换为实体框架 [英] Converting raw SQL queries into entity framework

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

问题描述

我现在正在学习实体框架。我的大多数SQL查询都是原始的SQL查询。我被建议使用实体框架,我想将我拥有的每个原始sql查询转换为实体框架。



现在,我想知道如何在asp.net中使用实体框架插入,更新和删除。我的问题是,我在下面提供的以下原始SQL查询的等效实体框架查询是什么?实体框架中还有sql参数吗?



1.)DELETE S FROM stringInstrumentItem S JOIN brand B ON S.brandId = B.brandId WHERE B.name = @brand



2.)UPDATE [品牌] SET类型= @type,name = @ name,image = @image WHERE brandId = @brandId



3.)UPDATE SII SET SII.brandId = b.brandId FROM stringInstrumentItem SII内部联接品牌b ON SII.brandId = b.brandId AND b.name = @newName



4.)INSERT INTO品牌价值(@ brandId,@ type,@ name,@ image)



我尝试了什么:



截至目前,我能够转换我们使用的常规选择语句这个:



SELECT *来自品牌



到此:



list =(从ob在obj.brands中选择g).ToList();

I'm now learning about entity framework. Most of my sql queries are raw sql queries. I was advised to use entity framework instead and I want to convert every raw sql query that i have into entity framework.

Now, i wanted to know how to insert,update and delete using entity framework in asp.net. My question is, what is the equivalent entity framework query of the following raw sql queries that i have provided below? Also is there sql parameters in entity framework?

1.) "DELETE S FROM stringInstrumentItem S JOIN brand B ON S.brandId = B.brandId WHERE B.name = @brand"

2.) "UPDATE [brand] SET type = @type, name = @name, image = @image WHERE brandId = @brandId"

3.) "UPDATE SII SET SII.brandId = b.brandId FROM stringInstrumentItem SII inner join brand b ON SII.brandId = b.brandId AND b.name = @newName"

4.) "INSERT INTO brand VALUES (@brandId,@type,@name,@image)"

What I have tried:

As of now, i am able to convert the regular select statement that we use like this:

SELECT * from brand

into this:

list = (from g in obj.brands select g).ToList();

推荐答案

嘿,

按照以下链接,您可以了解上述操作



获取,插入,使用数据库优先模型方法更新,删除数据使用ADO.NET Entity Framework 3.5和Visual Studio 2008. [ ^ ]



谢谢
Hey,
Follow the below link you can get an idea for the above operations

Fetch, Insert, Update, Delete Data with “Database First Model Approach” using ADO.NET Entity Framework 3.5 and Visual Studio 2008.[^]

Thanks


理想情况下,您希望使用LINQ进行查询,但是,在某些情况下您需要运行原始SQL查询,即使可以使用实体框架。



看看[ ^ ]



对于使用实体框架的CRUD操作,请参考[ ^ ]
Ideally you would like to use LINQ for queries, however, there are situations where you need to run raw SQL queries even that is possible with entity framework.

Take a look at [^]

For CRUD operations using entity framework, refer [^]


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

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