I'm glad to set common cache for almost all the entity like the following with Fluent NHibernate:
public static FluentConfiguration Create(string connStr)
{
return Fluently.Configure()
.Cache(x => x.UseSecondLevelCache().ProviderClass<SysCacheProvider>())
.Mappings(m =>
{
m.FluentMappings
.AddFromAssemblyOf<UserMap>()
.Conventions.Add(
// here the cache is set ReadWrite
Cache.Is(x => x.ReadWrite()),
);
})
}
But there is still some entities that I DO NOT want to cache them. How should I do?
public class ActivityMap : EntityMap<Activity>
{
public ActivityMap()
{
References<User>(m => m.Executor);
//Note: I'm not want to change the cache but REMOVE the cache!
//Cache.ReadOnly();
}
}
I'm not sure it's a real solution or just a walk around that is to set the expiration=0 in the cache region node in web.config, but whatever it works.
Related
I have the following convention which I load into my FNH config
public class TableNameConvention : IClassConvention, IClassConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
{
criteria.Expect(x => x.TableName, Is.Not.Set);
}
public void Apply(IClassInstance instance)
{
var tableName = instance.EntityType.Name.Pluralise();
instance.Table(tableName);
}
}
I do not specify table names on any of my mappings, yet this convention is not applied. I'm using Fluent NHibernate 1.4.1.1. Can anyone spot anything I might have done wrong?
UPDATE
The conventions are loaded in the following manner:
public static NHibernate.Cfg.Configuration BuildConfiguration()
{
var connectionStringName = "mydb";
return Fluently.Configure(new NHibernate.Cfg.Configuration())
.Database(MsSqlConfiguration
.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName))
.Dialect<MsSql2008Dialect>()
.AdoNetBatchSize(50))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<Profile>();
m.FluentMappings.Conventions.Add(DefaultLazy.Always(), DynamicUpdate.AlwaysTrue(), DynamicInsert.AlwaysTrue());
m.FluentMappings.Conventions.AddFromAssemblyOf<HiLoConvention>();
})
.ExposeConfiguration(config => config.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, typeof(ManagedWebSessionContext).AssemblyQualifiedName))
.ExposeConfiguration(HiLoConvention.CreateScript)
.ExposeConfiguration(RunSchemaUpdate)
.BuildConfiguration();
}
All conventions sit in the same assembly and namespace as the HiLoConvention referenced above in the .AddFromAssembly() method call.
UPDATE 2:
The problem is in the Accept() method, because if I remove this method (and also the IClassConventionAcceptance interface from the class declaration) then the convention is applied. I have also tried this expectation to no avail
criteria.Expect(x => string.IsNullOrEmpty(x.TableName))
The original code worked with Fluent 1.2.1...
This question is old, but perhaps this can help someone else:
I assume you wanted to set the convention on each entity, unless a table name was specified explicitly in the map. So to achieve that, you can simply do the following:
public class TableNameConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
var tableName = instance.EntityType.Name.Pluralise();
instance.Table(tableName);
}
}
This will apply the convention on all entities, unless TableName was specified explicitly in the map.
Have you tried
m.FluentMappings.ConventionDiscovery.AddFromAssemblyOf<HiLoConvention>()
in place of
m.FluentMappings.Conventions.AddFromAssemblyOf<HiLoConvention>()
I'm trying to set an unique constraint on the column LoginName of table User. So the following is my code:
public FluentConfiguration GetNHConfig()
{
var cfg = new FreeflyingConfiguration();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(_connStr))
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<LogOfArticle>(cfg))
.Add(AutoMap.AssemblyOf<LogOfUser>(cfg))
// here is the problem, I think
.Add(AutoMap.AssemblyOf<User>(cfg).UseOverridesFromAssemblyOf<UserMappingOverride>())
.Add(AutoMap.AssemblyOf<Role>(cfg))
.Add(AutoMap.AssemblyOf<Profile>(cfg))
.Add(AutoMap.AssemblyOf<Blog>(cfg))
.Add(AutoMap.AssemblyOf<Comment>(cfg)));
}
public class UserMappingOverride : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
// breakpoint is set here and can be hit every time
mapping.Map(x => x.LoginName).Not.Nullable();
mapping.Map(x => x.Email).Unique();
mapping.Map(x => x.Profile.BlogUrl).Unique();
}
}
The tables can be generated, and the breakpiont on line "" can be hit every time. But there is no change in database, which means, generate the Unique constraint, whatever I use UpdateSchema() or BuildSchema().
BTW, no error when executing.
So it's harder to find the reason. But any suggestion is welcome!
Are User, Role, Profile etc really in different assemblies? If they aren't you only need one .Add(AutoMap.AssemblyOf<T>(). This could be your problem because it might be overwriting your overrides on subsequent calls. I'm not all that familiar with overrides but it's worth a shot.
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(_connStr))
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<User>(cfg)
.UseOverridesFromAssemblyOf<UserMappingOverride>());
I have a application using NHibernate Auto Mapping... All working fine so far...
My Fluent Global.asax config:
private void InitializeNHibernateSession()
{
NHibernateSession.Init(
webSessionStorage,
new string[] { Server.MapPath("~/bin/Proj.Data.dll") },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath("~/NHibernate.config"));
}
But I need to map a class with Fluent mapping... I created the class :
namespace Proj.Data.NHibernateMaps
{
public class CategoryMap : IAutoMappingOverride<Category>
{
public void Override(AutoMapping<Category> mapping)
{
mapping.Id(x => x.Id)
.GeneratedBy.Identity();
mapping.Map(x => x.Description);
mapping.Map(x => x.UrlName);
mapping.References(x => x.ParentCategory)
.Not.LazyLoad();
}
}
}
The problem is that this mapping is never used by the NHibernate... Instead it uses the Auto Mapping generated Category...
How can I use my Fluent Mapping ?
Thanks
Paul
Wherever you're configuring the AutoPersistenceModel you need to reference the mapping overrides. I find the easiest way to do this is to just point it at the assembly containing the mapping overrides and let it discover all of them. That way you can add new IAutoMappingOverride implementations and it will be automatically picked up. You do this using the UseOverridesFromAssemblyOf extension method.
public class AutoPersistenceModelGenerator {
public AutoPersistenceModel Generate() {
return AutoMap.AssemblyOf<Category>()
.UseOverridesFromAssemblyOf<CategoryMap>();
}
}
I'm having some problems with getting the following convention to work:
public class ColumnNameUpperConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
string cName = instance.Property.Name.ToUpper();
instance.Column(cName);
}
}
What I'm wanting the above code to do is to map a property called "Modified" to a column named "MODIFIED".
And this is my config:
var config =
Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString(c => c.FromConnectionStringWithKey(cstringName)))
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<AgilityObject>();
m.AutoMappings.Add(
AutoMap.AssemblyOf<AgilityObject>(mappingConfiguration)
.Conventions.Add<ColumnNameUpperConvention>());
})
.ExposeConfiguration(x => x.SetProperty("current_session_context_class", "thread_static"))
.ExposeConfiguration(x => x.SetProperty("generate_statistics", "true"))
.BuildSessionFactory();
When I debug I can see that my convention code gets executed, but it doesn't seem like it actually does anything.
Am I missing something?
PS. Do the mappings I've set explicitly in a ClassMap automatically override conventions? There are exceptions to the above convention and I want to map those properties by hand.
My bad. I needed to use IAutoMappingOverride instead of ClassMap if I am to use AutoMapping at all.
Question says it all really, the default is for it to map as a string but I need it to map as an int.
I'm currently using PersistenceModel for setting my conventions if that makes any difference.
Update
Found that getting onto the latest version of the code from the trunk resolved my woes.
The way to define this convention changed sometimes ago, it's now :
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
So, as mentioned, getting the latest version of Fluent NHibernate off the trunk got me to where I needed to be. An example mapping for an enum with the latest code is:
Map(quote => quote.Status).CustomTypeIs(typeof(QuoteStatus));
The custom type forces it to be handled as an instance of the enum rather than using the GenericEnumMapper<TEnum>.
I'm actually considering submitting a patch to be able to change between a enum mapper that persists a string and one that persists an int as that seems like something you should be able to set as a convention.
This popped up on my recent activity and things have changed in the newer versions of Fluent NHibernate to make this easier.
To make all enums be mapped as integers you can now create a convention like so:
public class EnumConvention : IUserTypeConvention
{
public bool Accept(IProperty target)
{
return target.PropertyType.IsEnum;
}
public void Apply(IProperty target)
{
target.CustomTypeIs(target.PropertyType);
}
public bool Accept(Type type)
{
return type.IsEnum;
}
}
Then your mapping only has to be:
Map(quote => quote.Status);
You add the convention to your Fluent NHibernate mapping like so;
Fluently.Configure(nHibConfig)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.FluentMappings
.ConventionDiscovery.AddFromAssemblyOf<EnumConvention>();
})
./* other configuration */
Don't forget about nullable enums (like ExampleEnum? ExampleProperty)! They need to be checked separately. This is how it's done with the new FNH style configuration:
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
this is how I've mapped a enum property with an int value:
Map(x => x.Status).CustomType(typeof(Int32));
works for me!
For those using Fluent NHibernate with Automapping (and potentially an IoC container):
The IUserTypeConvention is as #Julien's answer above: https://stackoverflow.com/a/1706462/878612
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
The Fluent NHibernate Automapping configuration could be configured like this:
protected virtual ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.AutoMappings
.Add(CreateAutomappings);
}
).BuildSessionFactory();
}
protected virtual IPersistenceConfigurer SetupDatabase()
{
return MsSqlConfiguration.MsSql2008.UseOuterJoin()
.ConnectionString(x =>
x.FromConnectionStringWithKey("AppDatabase")) // In Web.config
.ShowSql();
}
protected static AutoPersistenceModel CreateAutomappings()
{
return AutoMap.AssemblyOf<ClassInAnAssemblyToBeMapped>(
new EntityAutomapConfiguration())
.Conventions.Setup(c =>
{
// Other IUserTypeConvention classes here
c.Add<EnumConvention>();
});
}
*Then the CreateSessionFactory can be utilized in an IoC such as Castle Windsor (using a PersistenceFacility and installer) easily. *
Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(() => CreateSessionFactory()),
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifestylePerWebRequest()
);
You could create an NHibernate IUserType, and specify it using CustomTypeIs<T>() on the property map.
You should keep the values as int / tinyint in your DB Table. For mapping your enum you need to specify mapping correctly. Please see below mapping and enum sample,
Mapping Class
public class TransactionMap : ClassMap Transaction
{
public TransactionMap()
{
//Other mappings
.....
//Mapping for enum
Map(x => x.Status, "Status").CustomType();
Table("Transaction");
}
}
Enum
public enum TransactionStatus
{
Waiting = 1,
Processed = 2,
RolledBack = 3,
Blocked = 4,
Refunded = 5,
AlreadyProcessed = 6,
}