How to map more than one tier of subclasses to nhibernate entity (with bycode)? - nhibernate

I am trying to setup some mappings and am getting this exception:
Cannot extend unmapped class: CommonEntity
[MappingException: Cannot extend unmapped class: CommonEntity]
NHibernate.Cfg.XmlHbmBinding.ClassBinder.GetSuperclass(String
extendsName) +217
NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping
mappingSchema, IDictionary`2 inheritedMetas) +352
NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping
mappingSchema) +85
NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping
mappingDocument, String documentFileName) +156
I have 3 classes. Entity, CommonEntity and User. Theres no entity or commonentity table, only a User table. User inherits from CommonEntity and CommonEntity inherits from Entity. Entity and CommonEntity are abstract.
I have defined this mapping:
public class Mapping : ConventionModelMapper
{
public Mapping()
{
IsRootEntity((type, declared) =>
{
return typeof(Entity<Guid>) == type.BaseType;
});
IsEntity((x,y) => typeof(Entity<Guid>).IsAssignableFrom(x) && !x.IsAbstract && !x.IsInterface);
Class<Entity<Guid>>(x =>
{
x.Id(c => c.Id, m=>m.Generator(Generators.GuidComb));
x.Version(c=>c.Version, (vm) => { });
});
}
}
Which is used like this:
var types = typeof(Mapping).Assembly.GetExportedTypes().Where(t => typeof(Entity<Guid>).IsAssignableFrom(t));
var mapping = new Mapping().CompileMappingFor(types);
configuration.AddMapping(mapping);
Both User and CommonEntity are in the "types" array. I have tried adding a mapping for CommonEntity too but it made no difference.
Class<CommonEntity>(x =>
{
x.Property(c => c.DateCreated, m => m.Type<UtcDateTimeType>());
x.Property(c => c.DateModified, m => m.Type<UtcDateTimeType>());
});
Also tried calling Subclass instead of Class. If i inherit User directly from Entity everything works fine. Any help?

The problem appears to have been that CommonEntity was meeting the requirement for IsRootEntity. I modified it like so and things seem to be working now.
IsRootEntity((type, declared) =>
{
return !type.IsAbstract &&
new[] {typeof (Entity<Guid>), typeof (CommonEntity)}.Contains(type.BaseType);
});

Related

Check if entity is loaded using AsNoTracking

Is there a way to check if the entity was loaded using AsNoTracking() or not?
As you know, the following code will not work for entity loaded using AsNoTracking().
ef.Entry(db).Collection(p => p.tblProducts).Load();
ef.Entry(db).Collection(p => p.tblOrders).Load();
...
...
...
Therefore, if the entity "db" was loaded using AsNoTracking() then I will do the following for loading its children.
db.tblProducts = ef.tblProducts.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
db.tblOrders = ef.tblOrders.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
...
...
...
I know it may not be a good approach, but if entity "db" was loaded using AsNoTracking(), then I know that its children do not need to be tracked too.
The question is, how to find out if the entity (that passed in the function) was loaded using AsNoTracking() or not.
POSSIBLE SOLUTION
I found this post here EntityFramework Code First - Check if Entity is attached, someone post the answer like this
public bool Exists<T>(T entity) where T : class
{
return this.Set<T>().Local.Any(e => e == entity);
}
So, can I use
if (Exists(db))
{
ef.Entry(db).Collection(p => p.tblProducts).Load();
ef.Entry(db).Collection(p => p.tblOrders).Load();
...
...
...
}
else
{
db.tblProducts = ef.tblProducts.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
db.tblOrders = ef.tblOrders.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
...
...
...
}
What do you think?
Thanks!
Thanks to this post EntityFramework Code First - Check if Entity is attached, I create a DbContext extension (as suggested on the link).
public static bool Exists<TEntity>(this DbContext ctx, TEntity entity)
where TEntity : class
{
return ctx.Set<TEntity>().Local.Any(e => e == entity);
}
And it worked nicely!
if (ef.Exists(db))
{
ef.Entry(db).Collection(p => p.tblProducts).Load();
ef.Entry(db).Collection(p => p.tblOrders).Load();
...
...
...
}
else
{
db.tblProducts = ef.tblProducts.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
db.tblOrders = ef.tblOrders.AsNoTracking().Where(x => x.WarehouseId == db.WarehouseId).ToList();
...
...
...
}
I hope this post could help someone with similar problem.
Cheers!

Ninject Get<T> WhenTargetHas<T>

So I'm using Ninject, specifically the contextual binding as follows :
Bind<IBlah>().ToMethod(x => FirstBlahProvider.Instance.GiveMeOne()).WhenTargetHas<FirstAttribute>().InRequestScope();
Bind<IBlah>().ToMethod(x => SecondBlahProvider.Instance.GiveMeOne()).WhenTargetHas<SecondAttribute>().InRequestScope();
I need to use the Kernel to get a given instance and would like to do it based on the Condition WhenTargetHas<T>. Something like the following would be great.
var myblah = Kernal.Get<IBlah>(x => x.HasWithTarget<FirstAttribute>)
How can you retrieve an instance based on the condition?
Worked out the answer :
Best to avoid using WhenTargetHas<T> instead use WithMetaData(key, value)
So
Bind<IBlah>().ToMethod(x => FirstBlahProvider.Instance.GiveMeOne()).WhenTargetHas<FirstAttribute>().InRequestScope();
Bind<IBlah>().ToMethod(x => SecondBlahProvider.Instance.GiveMeOne()).WhenTargetHas<SecondAttribute>().InRequestScope();
Becomes :
Bind<IBlah>().ToMethod(x => FirstBlahProvider.Instance.GiveMeOne()).WithMetaData("Provider", "First);
Bind<IBlah>().ToMethod(x => SecondBlahProvider.Instance.GiveMeOne()).WithMetaData("Provider", "Second");
You then need to create an Attribute which inherits the Ninject ConstraintAttribute and use that attribute in your constructor arguement.
As :
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
public class FirstProviderConstraint : ConstraintAttribute
{
public override bool Matches(IBindingMetadata metadata)
{
return metadata.Has("Provider") && metadata.Get<string>("Provider") == "First";
}
}
You then use it in a constructor arg as :
public class Consumer([FirstProviderConstraint] IBlah)
{
...
}
Or resolving from the Kernel
Get<ISession>(metaData => metaData.Get<string>(BindingKeys.Database) == BindingValues.OperationsDatabase)
I need to resolve scoping but that's how you satisfy both Constructor injection and explicit resolution from the Kernel when you have more than one binding.

mapping by code in nhibernate

I'm examin some mapping examples which uses mapping by code, and I have one simple question
If I have two properties which are mapped like this
Property(x => x.UserName, m =>
{
m.Length(50);
m.NotNullable(true);
});
Property(x => x.UpperUserName, m =>
{
m.Length(50);
m.NotNullable(true);
m.UniqueKey(“UniqueUpperUserName”);
m.Access(Accessor.Field);
});
What this m.Access(Accessor.Field); means?
And why it's used on these second property UpperUserName and not in the first one?
Thanks.
It means that NHibernate won't use the property itself when reading and writing values, but the underlying field.
// This will be used
var string upperUserName;
public string UpperUserName
{
get { return upperUserName; }
// Maybe this is a read-only property,
// so we must allow NHibernate to update the value somehow
// set { upperUserName = value; }
}
You can read more on available access types in NHibernate documentation. Just scroll down to Access and Naming strategies tables.

NHibernate JoinQueryOver with a non-visible property throws exception

I'm trying to do this:
Key key = session.QueryOver<Key>()
.Left.JoinQueryOver<ConfigValue>(x => x.ConfigValues)
.Where(c => c.Id == cfgValue.Id)
.SingleOrDefault();
But I get this exception:
NHibernate.QueryException was unhandled by user code
Message=could not resolve property: ConfigValues of: Domain.Model.Key
I figure it's because the Key object is declared in a way to restrict access to IList and mapped with a non-visible property.
public class Key
{
public virtual int Id { get; protected set; }
public virtual IEnumerable<ConfigValue> ConfigValues { get { return _configValues; } }
private IList<ConfigValue> _configValues = new List<ConfigValue>();
...
And mapped by code as:
Bag<ConfigValue>("_configValues", attr => {
attr.Lazy(CollectionLazy.Lazy);
attr.Inverse(false);
attr.Cascade(Cascade.None);
}, cm => cm.ManyToMany());
The question: How can I do it using NHibernate API?
The only way I managed to do it is with HQL:
IList<Key> keys = session.CreateQuery(#"select K_
from Key as K_
left outer join K_._configValues as KO_
where KO_.Id = :cfgValueId ")
.SetParameter("cfgValueId", cfgValue.Id)
.List<Key>();
I'm not firm with mapping by code but something along the lines
Bag<ConfigValue>("ConfigValues", attr => {
attr.Access("field.camelcase-underscore");
}, cm => cm.ManyToMany());
or Fluent NHibernate (if someones interested)
HasMany(x => x.ConfigValues)
.Access.CamelCaseField(Prefix.Underscore);

Mapping Enum as string in NHibernate 3.2 mapping by code

Using NHibernate 3.2 mapping by code (not fluent-nhibernate), I'm trying to map an Enum field to a string column instead of the default int representation. I can't get the right syntax.
For example:
public class Account {
public enum StateType { Pending, Active, Cancelled, Suspended }
...
public virtual StateType State { get; set; }
...
}
In the XML mapping, you can use NHibernate.Type.EnumStringType (see this link), but how do I do it in mapping by code?
NHibernate.Mapping.ByCode.ModelMapper mapper = new NHibernate.Mapping.ByCode.ModelMapper();
mapper.Class<Account>(map => {
map.Id(x => x.Id, attr => {
attr.Column("id");
attr.Generator(NHibernate.Mapping.ByCode.Generators.Identity);
});
// Default 'int' mapping
//map.Property(x => x.State);
// Cannot implicitly convert type 'StateType' to 'NHibernate.Type.EnumStringType'
//map.Property<NHibernate.Type.EnumStringType<Account.StateType>>(x => x.State);
Update:
Using this mapping, I managed to get it to save as a string to the DB, but I now get an exception when loading from the DB to the object model.
map.Property(x => x.State, attr => { attr.Type(NHibernateUtil.String); });
This is the exception I get when trying to load the object:
Invalid Cast (check your mapping for property type mismatches); setter of Model.Account
Got it! The following syntax works:
map.Property(x => x.State, attr => attr.Type<NHibernate.Type.EnumStringType<Account.StateType>>());