开发者

RavenDB modelling 1 to many documents

开发者 https://www.devze.com 2023-04-12 05:50 出处:网络
Take this example model: public class Location { public string Id { get; set; } public string Name { get; set; }

Take this example model:

public class Location
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class Activity
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

The relationship between these documents is a location can have none or many activities. Say London can have Paintball, Go karting etc.

I did thing to put these documents into one document but watching and reading the documentation and blogs, the documents need to be accessed separately. You can imagine a website page for each location listing activities and activity pages giving the details.

Example queries would be:

  • Fetch all locations
  • Fetch a locations details (given Location name from URL)
  • Fetch all activities per location (given Location name from URL)
  • Fetch an activities details (given Location name & Activity name from URL)

Using the DenormalizedReference method described at:

http://blogs.sonatribe.com/wayne/2011/07/06/using-denormalized-references-in-ravendb/

(and I did see this but coul开发者_高级运维dn't figure out the syntax or if it does what I need

http://ravendb.net/faq/denormalized-references )

Seems to kind of fit my scenarios. But when making a demo project with the above types I noticed if I changed a location name it didn't change it in the activity. Keeping the documents in sync is a big thing.

So how do I achieve this?


Phil,

  • have a string LocationId property in the Activity, that would allow you to query for them.
  • it would be easier probably to just store the activities as a collection inside the Location (so they aren't a separate document, but are embedded inside).
0

精彩评论

暂无评论...
验证码 换一张
取 消