简体   繁体   English

首先通过WCF使用EF数据库执行查询

[英]Performing Queries using EF Database first through WCF

I'm new to .NET development and I have a backend DB on SQL server which I have generated from Database with Entity Framework. 我是.NET开发的新手,我在SQL Server上有一个后端数据库,该数据库是从带有Entity Framework的数据库生成的。

I now want to access the data using WCF - How does one query a database through a windows service or, in general, query a database first DB? 我现在想使用WCF访问数据-如何通过Windows服务查询数据库,或者通常如何首先查询数据库?

Thanks 谢谢

The simple answer is that you should look to abstract away your database logic behind your web services. 简单的答案是,您应该在Web服务之后抽象数据库逻辑。 For example each WCF web service should provide CRUD (create read update delete) functions for a given entity (eg user). 例如,每个WCF Web服务都应为给定实体(例如,用户)提供CRUD(创建读取更新删除)功能。

I assume you have made an EF model with your db? 我假设您已经用数据库制作了EF模型?

EF will have created a Context class for you (probably a DbContext depending on the version of EF used). EF将为您创建一个Context类(可能是DbContext具体取决于所用EF的版本)。

You can then instantiate an object of this class, and perform operations against it such as querying. 然后,您可以实例化此类的对象,并对它执行诸如查询之类的操作。 It will contain the DbSet collections matching your tables/collections defined in your model. 它将包含与您的模型中定义的表/集合匹配的DbSet集合。

So, for example, I have created a model which contains one table called 'Products' and I can perform queries against the db using the generated context like this: 因此,例如,我创建了一个模型,其中包含一个名为“ Products”的表,并且可以使用生成的上下文对数据库执行查询,如下所示:

var context = new MyGeneratedContextClass();
var results = context.Products.Where(x=>x.ProductId.Equals(42));

签出WCF数据服务 ,这使您可以非常轻松地将Entity Framework模型(或其一部分)公开为REST / OData Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM