I'm trying to create a
public class BaseMap<T> : ClassMap<T>
for my application. It should support localized entities, which their primary key must be both Id & Language:
CompositeId()
.KeyProperty(x => x.Id)
.KeyProperty(x => ((ILocalizedEntity)x).Language);
Another thing I want is one localized class to be able to reference another localized class.
I've done the following (After a lot of research, This is all I have):
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language");
}
return base.References(memberExpression);
}
This gave me IndexOutOfRange exception when trying to insert, the reason was that the "Language" property was mapped twice, but there was only 1 language column in the DB, so I did this:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
Which resolved the IndexOutOfRange problem, but didn't accomplished what I want. because it always inserts NULL to the column "CategoryId", because I specified Not.Insert() and Not.Update(), so it doesn't!
Now, I am in a situation I want it to map "CategoryId" but not "Language" because it's already mapped, as it's part of the ComposideId (primary key).
So I gave a try to this:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Nullable()
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
Whishing it will not insert or update only "Language" and not "CategoryId" - but no luck there either.
I've also tried to make language first in the PK:
CompositeId()
.KeyProperty(x => ((ILocalizedEntity)x).Language)
.KeyProperty(x => x.Id);
And changed the Reference to:
return base.References(memberExpression)
.Columns("Language")
.Not.Update()
.Not.Insert()
.Columns("CategoryId")
.Nullable();
But still the "Not.Insert()" and "Not.Update()" is affecting both "Language" & "CategoryId".
I tried mapping the "CategoryId" before the "References" call, like this:
Map(memberExpression, "Language");
return base.References(memberExpression)
.......
But it failed.
Anyone has any idea, how to reference a class with 2 columns CompositeId from a class with 2 columns CompositeId when 1 of the columns is common to the primary key and the foreign key?