简体   繁体   中英

WCF data service in wcf class library (Have clients talk to database through the service)

I am creating a simple application where clients need to talk to a server in order to perform simple tasks. The server consists of a database and a WCF service. The clients connect to the service and to the database.

I am using ado.net entity data model to create CRUD operations on the database. I will like to have the clients create CRUD operations through the service and avoid having the clients connect to the database.

when I create methods on the service that return entity objects I get an exception. For example I have the method:

    public Data.Scheduling[] GetSchedules()
    {
        Data.TCEntities1 entities = new Data.TCEntities1();

        var a = entities.Schedulings.ToArray();

        return a;
    }

I don't want to recreate the Schedulings class and other entity classes in order to make my methods work. How will I be able to expose create, read, update and delete operations through my wcf class library service without having the clients connect to the database?

Edit

More explanation of my problem in case it is confusing:

In my server application (wcf service) I have basic functionality plus the ado.net entity data classes. Because I have the ado.net entity data classes I am able to extract results from the database very easily such as by doing:

    Data.TCEntities1 entities = new Data.TCEntities1();
    var a = entities.Schedulings.ToList(); // schedulings is a table in my database

as you can see these line will retrieve all scheduling rows from the table schedulings in the database and It will be essay to work with (I will have a List ). Why do I get an error if I have my wcf service return a List<Data.Scheduling> ? I want to prevent the clients from queering the database as they just perform very simple operations (database has only 2 tables).

WCF works by exposing contracts. Because you use ADO.NET datamodel, you will get auto generated 'partial' classes. These are not marked with the DataContract or DataMember which is required by WCF to let them work on the client side. One way or another, you need to copy your partial class into a new class/file and add the attributes so they will show up on the client.

[DataContract]
public partial class Entity1
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Property1 { get; set; }

    [DataMember]
    public string Property2 { get; set; }
}

This is confusing question. You create the CRUD WCF calls and client calls the CRUD functions. Service talks to database. Client talks to service. Client doesn't talk with database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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