NHibernate Free / Cascading Foreign Key

The problem arises when creating cascading foreign keys using the following models, in which only one model (UserAddition) knows another (User), and there is no way to add the UserAddition property to the User class:

class User {
    public virtual Guid Id { get; set; }
    // Other fields of no relevance
}

class UserAddition {
    public virtual Guid Id { get; set; }
    public virtual User RemoteUser {get; set; }
    public virtual string AdditionalData {get; set; }
}

The generated SQL table for UserAddition must have a foreign key for the user that is set to ON DELETE CASCADE (setting ON UPDATE is not important).

When using the following mapping class, the foreign key is always set to "No action", even if it indicated otherwise in the mapping. Did I miss something?

public class UserAdditionMapping : ClassMap<UserAddition>
{
    public TrainerToEmployeeMapping()
    {
        this.Id(x => x.Id);
        this.References(x => x.RemoteUser).ForeignKey().Cascade.All();
    }
}

The database used is Microsoft SQL Server 11.

Thank.

+3
source share
2

:

this.References(x = > x.RemoteUser).Column( "YourColumnName" ). Cascade.All();

0

NHibernate, ON DELETE CASCADE . :

NHibernate on delete cascade .

NHibernate.Mapping.Collection:

public virtual void Validate(IMapping mapping)
{
    if (Key.IsCascadeDeleteEnabled && (!IsInverse || !IsOneToMany))
    {
      throw new MappingException(string.Format(
          "only inverse one-to-many associations may use on-delete=\"cascade\": {0}", Role));
    }
 ...

, , :

HasMany(x => x.UserAdditions)
   ...
   // the setting
   .ForeignKeyCascadeOnDelete()

User. , , ( ), , ...

0

All Articles