c# - 使用 WCF 和 Entity Framework 的客户端服务器

标签 c# wcf entity-framework client-server

我正在开发一个业务应用程序,出于我无法控制的原因,该应用程序必须使用客户端服务器架构。

即客户端都连接到一个应用服务器,应用服务器连接到数据库等。

为此,我过去创建了一个 WCF 服务,它公开了数据库的 CRUD 类型方法。像这样的方法存在于 WCF 中:

Customer GetCustomer(int customerId);
List<Customer> GetAllCustomers();
etc...

但是我总是发现同样的 2 个问题:

1) 有很多管道代码连接:客户端 -> 应用服务器 -> 数据库服务器

2) 当客户端应用程序需要获取更复杂的数据时,我最终不得不在服务器端添加方法,这最终导致了如下可怕的事情:

Customer GetCustomerByNameWhereCustomerHasBoughtProduct(string name, int productCode);

或者返回比需要更多的数据并在客户端处理。这对数据库来说很慢而且非常糟糕。像这样的东西:

List<Customer> customers = _Service.GetAllCustomers();
List<Product> products = _Service.GetAllProducts();

List<Customer> customersWhoBoughtX = (from c in customers where   
                         c.OrderLog.Contains(products.Where(p => p.Code == x)
                         select c).ToList()

我在这里做错了什么,因为这必须以某种方式解决。

有没有办法使用约定通过 wcf 服务公开数据库?或者任何其他可以帮助我正在做的事情的想法?

理想情况下,我会说客户端可以直接连接到数据库,但我被告知这是一个无法更改的问题。

我非常感谢一些指点。

谢谢

最佳答案

考虑使用 OData 公开您的实体。然后,您可以在客户端上以类似于编写 EF LINQ 查询的方式编写 LINQ 查询。这是一篇包含详细信息的文章:

http://www.vistadb.net/tutorials/entityframework-odata-wcf.aspx

关于c# - 使用 WCF 和 Entity Framework 的客户端服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663002/

相关文章:

asp.net-mvc - 我真的需要ORM吗?

c# - 解决 "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection"InvalidOperationException

c# - 从 IObservable 通过 LINQ 创建匿名对象?

c# - 为什么WCF代理对象无法访问全局静态对象?

json - WCF 数据服务 5.0 返回 POCO 的解决方法?

wcf - 如何在 ViewModel 中注入(inject) wcf 客户端依赖项并使其可测试?

c# - 如何将项目的内容从 WPF 组合框拖放到 WPF 文本框

c# - ASP :Button is not calling server-side function

c# - 转换十进制?加倍?

asp.net-mvc - 返回 IQueryable 时何时/如何处置 DbContext?