What kind of parameters do I have to send to a WCF endpoint to insert an entry into a table that has a relationship with another table?

StackOverflow https://stackoverflow.com/questions/14225899

Domanda

I'm working with Visual Studio 2012 and developing on C#. I just started started working a WCF web service, I created the database through the Model First approach and so far I've been able to insert, update, delete and get entries into simple tables, but I've come across a problem: I don't know how to send the parameters for a table that has a relationship with another table.

To explain better my doubt here's an example: I have a Regions table, then I have this other table called Clusters, a region has many clusters and a cluster belongs to a region.

The resulting classes created by EF look like this:

public partial class Regions
{
    public Regions()
    {
        this.Clusters = new HashSet<Clusters>();
    }

    public int RegionId { get; set; }
    public string Name { get; set; }
    public string Point { get; set; }
    public System.DateTime CreatedDateTime { get; set; }
    public System.DateTime UpdatedDateTime { get; set; }

    public virtual ICollection<Clusters> Clusters { get; set; }
}

public partial class Clusters
{

    public int ClusterId { get; set; }
    public System.DateTime CreatedDateTime { get; set; }
    public System.DateTime UpdatedDateTime { get; set; }

    public virtual Regions Region { get; set; }
}

With this kind of relationship, how can I add a new Cluster? My endpoint for adding a new Cluster receives a string which is a JSON string, I deserialize it into a Cluster object. I deserialize like this:

 Clusters cluster = new JavaScriptSerializer().Deserialize<Clusters>(data); //data is the JSON string

In this case, the only information I'll be sending into the JSON string will be the Region to which the Cluster belongs to (the DateTime for when it is created the object is being added on the server side), but how do I send the Region information into the JSON so it can be added into the Cluster?

What I mean is, do I have to send a JSON string that looks like this?

{"RegionId":2}

Because if I do that, no entry is made, do I have to something else?

I'm really new to working with EF and WCF web services, any help will be appreciated.

È stato utile?

Soluzione

From what I gather, you have two methods of saving your cluseters. The first one is as a byproduct of the saving or regions. If you have basic CRUD WCF endpoints, you can simply construct your Region entity in your client, add the clusters, and then call the save.

var reg = new Region();
//set region properties
//build clusters set
for (int i = 0; i<5; i++)
    reg.Clusters.add (new Cluster {CreatedDateTime= DateTime.Now, 
                                   UpdatedDateTime = DateTime.Now});

wcfClient.SaveRegion(reg); //<-- if the back-end works with EF, this should be able 
                           //to insert one new region with five new clusters

Your other option is to save clusters through a WCF endpoint that is designed to save clusters themselves. Before doing this, I would recommend that you modify your Clusters entity to explicitly have a RegionId field.

public partial class Clusters
{

    public int ClusterId { get; set; }
    public System.DateTime CreatedDateTime { get; set; }
    public System.DateTime UpdatedDateTime { get; set; }

    public int RegionsId {get; set;}
    public virtual Regions Region { get; set; }
}

Entity framework should be able to recognize automatically that the RegionsId corresponds to the primary key of the Regions entity. After this, it's just a matter of making sure that every Cluster you create has a proper RegionsId property. When you call your "wcfClient.SaveCluster(cluster)" method, EF would automatically link that cluster to the proper region.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top