简体   繁体   English

通过WCF公开实体框架数据类型

[英]Exposing Entity Framework data types over WCF

Edit: I don't know how you guys do this, but this question is indeed a duplicate. 编辑:我不知道你们是怎么做到的,但是这个问题确实是重复的。 See above. 往上看。

at the moment what I have on my hands is a WCF service that exposes a few basic operations: 目前,我所掌握的是WCF服务,它公开了一些基本操作:

public interface IUniService
{
    [OperationContract]
    bool IsUser(string userName);

    [OperationContract]
    User GetUserByUsername(string userName);
}

I also have an Entity Framework database running. 我也有一个正在运行的实体框架数据库。 Here's a bit of code that accesses the server. 这是访问服务器的一些代码。

public bool IsUser(string userName)
{
    using (UniDBEntities db = new UniDBEntities())
    {
        User a = new User();
        var user = db.Users.Where(p => p.UserName == userName).ToList();
        return (user.Count() != 0);
    }
}

But the question I have is, I want to expose via the WCF service, some of the classes that are currently in my Entity Framework. 但是我的问题是,我想通过WCF服务公开我实体框架中当前包含的某些类。 When I write a function like: 当我编写类似的函数时:

User GetUserById(int id)

when I return the result, it doesn't seem as though my client has any idea what the User class is. 当我返回结果时,我的客户似乎并不知道User类是什么。 How do I expose my entities as Datamembers? 如何将我的实体公开为数据成员?

I don't think you can transparently disconnect EF objects from the db easily, however you can create a dto or poco and send that over the network. 我不认为您可以轻松地从数据库中透明地断开EF对象的连接,但是您可以创建dto或poco并通过网络发送。 For reads is not a problem, you send read-only data, for writing to the db, either create method dedicated to updating data, or you can take the EF object, send and receive a copy, suitably wrapped as poco/dto, and then update it with the changes. 对于读取来说不是问题,您可以发送只读数据,对于写入数据库,可以使用专用于更新数据的create方法,也可以采用EF对象,发送和接收副本,并适当地包装为poco / dto,并且然后使用更改进行更新。

It's probably better though to model your middle tier differently to the db schema however, make ita domain model so there's not a 1 to 1 mapping between wcf methods and EF objects. 最好以与db模式不同的方式对中间层进行建模,但是,请使它成为ita域模型,以便在wcf方法和EF对象之间没有一对一的映射。 Then you can make your wcf model more appropriate for the caller. 然后,您可以使wcf模型更适合于调用者。

Look up the repository pattern as an example. 查找存储库模式作为示例。

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

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