Problem with RIA Services and POCO classes, associated child collection not being sent
Here's my setup: I have some "data", which is actually a graph of objects (DTOs representing SSAS objects) that I want serialized and sent via RIA Services for consumption by a silverlight app. Here's a simple example of that:
public class Database
{
开发者_JS百科[Key]
public int ID { get; set;}
public string Name { get; set;}
[Include]
[Association("DatabaseCue", "ID", "DatabaseID"]
public IEnumerable<Cube> Cubes { get; set;}
}
public class Cube
{
[Key]
public int ID { get; set;}
public int DatabaseID { get; set;}
public string Name { get; set;}
// other Cube properties
}
And I have a DomainService
:
[EnableClientAccess()]
public class AmoService : DomainService
{
public IQueryable<Database> GetDatabases()
{
return myAmoAdapter.GetDatabases();
}
}
myAmoAdapter.GetDatabases()
retrieves the Database collection, filling also its child Cubes collection just fine, however, in the client side, the cubes collection is empty! I can't figure out how to make this work! (or maybe it just won't?!)
Anybody know why this isn't working and how to fix it? I would be grateful.
The only thing that appears different from how I do this is public IEnumerable Cubes { get; set;}. I would usually do that as:
public List<Cube> Cubes { get; set;}
Also, you need a Composition tag as follows:
[Include]
[Composition]
[Association("DatabaseCue", "ID", "DatabaseID"]
public IEnumerable<Cube> Cubes { get; set;}
Did you check on the client side if it has a an EntitySet
for Cube
?
If that is not available, then you also need to tell RIA Services that Cube
is a class that should be generated on the client. You can do this by defining a dummy method like this on the server side:
public IQueryable<Cube> GetCubesDummy()
{
return new List<Cube>().AsQueryable();
}
If the EntitySet
exists, you should also check if the Foreign-Keys are ok. Just because the collection is populated on the server, it doesn't mean that the DatabaseID
in the Cube
elements is correct.
You can explicitly set the IsForeignKey
property of the Association
attribute on the Database
object
[Include]
[Association("DatabaseCue", "ID", "DatabaseID", IsForeignKey = false]
public IEnumerable<Cube> Cubes { get; set;}
One thing you might want try is to initialize the Cubes collection on Database in the constructor.
public class Database
{
public Database
{
Cubes = new List<Cube>();
}
[Key]
public int ID { get; set;}
public string Name { get; set;}
[Include]
[Association("DatabaseCue", "ID", "DatabaseID"]
public IEnumerable<Cube> Cubes { get; set;}
}
RIA Services can sometimes exhibit some strange behavior if Associated Entity collections are not initialized.
I had a similar problem and it turned out that it would work if I added the child to the parent's collection and also specifically set the parent id.
So
Database db = ...
db.Cubes.Add(new Cube(){ID=1, DatabaseID=db.ID, Name = "Cube1" });
if I didn't have the DatabaseID=db.ID they would be set to zero and nothing would be returned by RIA Services. Bug?
精彩评论