I've tried that as well. Basically the issue is that there's a one-to-
many relationship from Address to Person (so a person can only have
one address, but an address can be assigned to multiple people), but
an Address should never exist without a Person (if an Address has no
people assigned to it, it should be deleted, because it means nothing
by itself).
My entities look like this now:
------------------------------------------------------------
[ActiveRecord]
public class Person : ActiveRecordLinqBase<Person>
{
[PrimaryKey]
public int Id { get; set; }
[BelongsTo(Cascade = CascadeEnum.All)]
public Address Address { get; set; }
}
[ActiveRecord]
public class Address : ActiveRecordLinqBase<Address>
{
[HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan,
Inverse = true)]
public ICollection<Person> Persons { get; set; }
[PrimaryKey]
public int Id { get; set; }
[Property]
public string Street { get; set; }
public Address()
{
Persons = new List<Person>();
}
}
------------------------------------------------------------
However, it is still not removing the old Address when I assign a new
Address to Person. I'm beginning to think that this is intended
behavior, and if I really want to do what it do, I need to do it
manually.
On Jan 8, 11:02 pm, Krzysztof Koźmic <[email protected]>
wrote:
> if multiple people can share an address than it's a one to many relation,
> and you can use ManyRelationCascadeEnum.AllDeleteOrphan
>
> On 2010-01-09 09:55, DanielT wrote:
>
> > Yes they do, because multiple people can share one address (like a
> > household of people).
>
> > I tried inverse = true on the HasMany attribute, but it's the same
> > result, 2 addresses created with one that's orphaned.
>
> > On Jan 8, 10:12 pm, Krzysztof Ko mic<[email protected]>
> > wrote:
>
> >> Does your address really needs to be an entity?
>
> >> Most of the time it's really a component, which means you wouldn't have
> >> such problems.
>
> >> Otherwise, set inverse = true.
>
> >> On 2010-01-09 08:55, DanielT wrote:
>
> >>> I have two entities, Person and Address. Here they are simplified:
>
> >>> ------------------------------------------------------------
>
> >>> [ActiveRecord]
> >>> public class Person : ActiveRecordLinqBase<Person>
> >>> {
> >>> [BelongsTo(Cascade = CascadeEnum.All]
> >>> public Address Address { get; set; }
> >>> }
>
> >>> [ActiveRecord]
> >>> public class Address : ActiveRecordLinqBase<Address>
> >>> {
>
> >>> }
>
> >>> ------------------------------------------------------------
>
> >>> I'm running the following code:
>
> >>> ------------------------------------------------------------
>
> >>> // Add initial person and address
> >>> var person = new Person();
> >>> person.Address = new Address();
> >>> person.Save();
>
> >>> // Change address
> >>> person.Address = new Address();
> >>> person.Save();
>
> >>> ------------------------------------------------------------
>
> >>> After running the first save, I get one Person and one Address saved
> >>> in the database, which is the expected behavior. However, after the
> >>> second save, I get one Person and TWO addresses, one of which is
> >>> orphaned. In the object-oriented world, this orphaned object would get
> >>> garbage collected, but in the database world, there's nothing that
> >>> says this Person object can't exist by itself. Semantically, it also
> >>> makes sense, since the Address belongs to the Person rather than the
> >>> other way around. However, in my object model, Address belongs to a
> >>> Person and shouldn't exist without one.
>
> >>> Does this mean I have to manually 'garbage collect' it? I would need
> >>> to introduce an 'ICollection<Person> Addresses' in Address and use
> >>> 'Person.Queryable.Where(x => x.Addresses.Count == 0)' to determine
> >>> whether it should delete itself or not. However, can Castle do this
> >>> for me automatically?
--
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en.