How do I override the cascade rule for a mapping covered by a convention?

22 views
Skip to first unread message

trul...@googlemail.com

unread,
Mar 31, 2009, 11:38:00 AM3/31/09
to Fluent NHibernate
I have two classes

public class Document
{
public virtual int Id { get; set; }
public virtual IList<File> Files { get; set; }
}
public class File
{
public virtual int Id { get; protected set; }
public virtual Document Document { get; set; }
}

with the following convention:

public class HasManyConvention : IHasManyConvention
{
public bool Accept(IOneToManyPart target)
{
return true;
}

public void Apply(IOneToManyPart target)
{
target.Cascade.All();
}
}

and these mapping overrides

public class DocumentMappingOverride : IAutoMappingOverride<Document>
{
public void Override(AutoMap<Document> mapping)
{
mapping.HasMany(x => x.Files)
.Inverse()
.Cascade.AllDeleteOrphan();
}
}
public class FileMappingOverride : IAutoMappingOverride<File>
{
public void Override(AutoMap<File> mapping)
{
mapping.References(x => x.Document).Not.Nullable();
}
}


I understand that I need to make an IClassConvention for Document to
change the cascade behaviour, however I can't get this to work!

If i do this:

public class DocumentConvention : IClassConvention
{
public bool Accept(IClassMap target)
{
return target.EntityType == typeof(Document);
}

public void Apply(IClassMap target)
{
target.SetAttribute("cascade", "all-delete-orphan");
}
}

I get: "The 'cascade' attribute is not declared."

If i do this:

public class DocumentConvention : IClassConvention
{
public bool Accept(IClassMap target)
{
return target.EntityType == typeof(Document);
}

public void Apply(IClassMap target)
{
target.HasMany<Document, File>(x => x.Files)
.Inverse()
.Cascade.AllDeleteOrphan();
}
}

Then I get: "Duplicate collection role mapping
Redcliffe.Domain.Entities.Document.Files"

so i added:

mapping.IgnoreProperty(x => x.Files);

to my document mapping, but then Files is always empty.

What am I doing wrong?

How can I override the cascade rule for a single HasMany relationship?

Thanks

Andrew

Reply all
Reply to author
Forward
0 new messages