Not necessarily "will never have" — it *is* possible to persuade NHibernate
to set ON DELETE CASCADE. Have a look at this Stack Overflow question; if a
collection is one-to-many and inverse, you can set Key(k =>
k.OnDelete(OnDeleteAction.Cascade)). My thinking was that if it's possible
in one case, perhaps there is a way to force the same behaviour in an even
more (IMHO) straight-forward instance.
Dawid
On Thursday, May 15, 2014 6:27:28 PM UTC+2, Ricardo Peres wrote:
>
> Meaning: if you want to have cascade actions on the database, you have to
> add them yourself, maybe using auxiliary database objects.
>
> RP
>
> On Thursday, May 15, 2014 5:26:27 PM UTC+1, Ricardo Peres wrote:
>>
>> Of course it doesn't, and it will never have! Cascade actions are
>> performed by NHibernate, not at the database level!
>>
>> RP
>>
>> On Thursday, May 15, 2014 4:50:11 PM UTC+1, Dawid Ciecierski wrote:
>>>
>>> Ricardo, thanks for your help but if you look closer at the resulting
>>> database (and/or the exported schema) you'll notice that the foreign key
>>> relationship has NO ACTION set for the ON DELETE event in the foreign key
>>> constraint. This works fine when executing everything via NHibernate's
>>> entity management methods, but fails when a simple DELETE is issued against
>>> the database.
>>>
>>> Dawid
>>>
>>> On Thursday, May 15, 2014 12:58:12 PM UTC+2, Ricardo Peres wrote:
>>>>
>>>> I have a similar mapping - Post and Tag - where one Post can have
>>>> several tags, which are just strings (sorry, don't have time to change it):
>>>>
>>>> this.Set(x => x.Tags, x =>
>>>> {
>>>> x.Key(y =>
>>>> {
>>>> y.Column("post_id");
>>>> y.NotNullable(true);
>>>> });
>>>> x.Cascade(Cascade.All);
>>>> x.Lazy(CollectionLazy.NoLazy);
>>>> x.Fetch(CollectionFetchMode.Join);
>>>> x.Table("tag");
>>>> }, x =>
>>>> {
>>>> x.Element(y =>
>>>> {
>>>> y.Column("tag");
>>>> y.Length(20);
>>>> y.NotNullable(true);
>>>> y.Unique(true);
>>>> });
>>>> });
>>>>
>>>>
>>>> I am using set instead of bag, this is generally better, and in your
>>>> case, I imagine you don't want duplicate values stored.
>>>> See if this works!
>>>>
>>>> RP
>>>>
>>>> On Thursday, May 15, 2014 11:31:13 AM UTC+1, Dawid Ciecierski wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> Let me start by asking the moderator to please look at my previous
>>>>> post sent around Friday (May 9), as it went into much greater detail as
>>>>> well as linked to a sample project but for some reason was not
>>>>> approved...
>>>>> Would be grateful if you could look at it again as I spend a good several
>>>>> minutes describing what I'm trying to accomplish as best I could.
>>>>>
>>>>> Also wanted to add that this is a sister post to one on the
>>>>> developers
>>>>> group<https://groups.google.com/forum/#!topic/nhibernate-development/UTbygrpFz04>,
>>>>>
>>>>> in which I tried to go into more technical aspects of this issue, but was
>>>>> still directed to this group :-)
>>>>>
>>>>> Without further ado thou. What I have is frequently updated and delete-d
>>>>> entity that is just a view on a more complex business domain. Each entity
>>>>> in this "view" has a child collection of simple strings (think tags).
>>>>> Because of the DELETE FROM [TableName] we regularly execute, I'd like
>>>>> to mark the foreign key relationship as ON DELETE CASCADE. Right now
>>>>> I do so manually but only because I've found no way to coerce NHibernate
>>>>> into seeing the relationship the same way. Ultimately I'd like to find a
>>>>> way to do so as the whole database is frequently rebuilt from scratch by
>>>>> SchemaExport and doing manual updates to multiple foreign key
>>>>> relationships
>>>>> is time-consuming. Below is a simple class with its mapping demonstrating
>>>>> the use case:
>>>>>
>>>>> public class Photo
>>>>> {
>>>>> public int Id { get; set; }
>>>>> public string Name { get; set; }
>>>>> public IList<string> Tags { get; set; }
>>>>>
>>>>> public Photo()
>>>>> {
>>>>> Tags = new List<string>();
>>>>> }
>>>>> }
>>>>>
>>>>> public class PhotoMap : ClassMapping<Photo>
>>>>> {
>>>>> public PhotoMap()
>>>>> {
>>>>> Lazy(false);
>>>>> Id(p => p.Id);
>>>>> Property(p => p.Name);
>>>>> Bag(
>>>>> p => p.Tags,
>>>>> collectionMapping: collectionMapping =>
>>>>> {
>>>>> //collectionMapping.Inverse(true);
>>>>> collectionMapping.Lazy(CollectionLazy.NoLazy);
>>>>> collectionMapping.Cascade(Cascade.All | Cascade.DeleteOrphans);
>>>>> },
>>>>> mapping: mapping =>
>>>>> {
>>>>> //mapping.OneToMany(); // NH does not like this
>>>>> mapping.Element(e => e.Column("tag_value"));
>>>>> });
>>>>> }
>>>>> }
>>>>>
>>>>> When I uncomment the mapping.OneToMany line NHibernate tells me that
>>>>> there is an unmapped entity System.String, so clearly I'm going against
>>>>> its
>>>>> way of thinking (and on the developers' group Ricardo confirmed that
>>>>> one-to-many is for entities only). Will be grateful for any advice on how
>>>>> I
>>>>> can make it work.
>>>>>
>>>>> Best regards,
>>>>> Dawid Ciecierski
>>>>>
>>>>> Ps. I've prepared a sample project that does not make actual database
>>>>> calls which I can link to, but will refrain myself from doing so just in
>>>>> case linking outside of Google Groups automatically puts my post in the
>>>>> moderator's trash.
>>>>>
>>>>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.