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.