How to change schema with Fluent NHibernate AutoMapping - fluent-nhibernate

In my database every table belongs to a schema (Person for example.) I am using Fluent NHibernate with Automapping and my question is how I set the schema I want to use.

Use a convention, specifically an IClassConvention.

public class SchemaConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Schema("schemaNameGoesHere");
}
}

Related

Can I tell nhibernate to swap a System.Data.DbType out for a custom IUserType completely?

I see how I can use the mapping file to specify a custom IUserType for any nhibernate mapped class I want.
However, I don't want to type it in every time. Is there a way to override the standard mapping table seen here?
I have Jørn Schou-Rode's IUserType implementation for storing a guid in Binary(16) in MariaDB. All I want is to enter one or two lines of code to tell Nhibernate when it sees a System.Guid to convert it to the custom "BinaryGuidType" that Schou-Rode made me. Can it be done?
If you are using Fluent NHibernate you can easily do this using Conventions. Here is how I map all strings to varchar instead of nvarchar:
public class PropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
SetStringsAsAnsiStringByDefault(instance);
}
private void SetStringsAsAnsiStringByDefault(IPropertyInstance instance)
{
if (instance.Property.PropertyType == typeof(string))
{
instance.CustomType("AnsiString");
}
else if (instance.Property.PropertyType == typeof(char))
{
instance.CustomType("AnsiChar");
}
}
}
I believe the later versions of NHibernate have in-built support for conventions, but the documentation seems to be sparse. Here is an article for you to get started though: http://weblogs.asp.net/ricardoperes/nhibernate-conventions

How can I stop Fluent NHibernate automapping from creating foreign keys across the database?

I am using latest version of Fluent NHibernate automapping. Is there any convention or property I can set to stop creating the foreign key constraints across all the tables? I have nearly 200 classes, So I cannot go to each individual class and property name and set
ForeignKeyConstraintNames("none", "none")
How can we add ForeignKeyConstraintNames("none", "none") in Automapping? I don't want to hardcode the table name or column name. I would like to have the AutoMapping create all the mappings without foreign keys. Basicall don't create any foreign keys across the database. How can we do this?
There is similar POST HERE but the answer was not clear to me.
a simple convention
public class NoForeignKeys : IReferenceConvention, IHasManyConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.ForeignKey("none");
}
public void Apply(IOneToManyCollectionInstance instance)
{
instance.Key.ForeignKey("none");
}
}
// use it
AutoMap.AssemblyOf().Conventions
.FromAssembly() or .Add(typeof(NoForeignKeys))

FluentNHibernate Mixing Fluent and Auto Mappings

Is there a clean way to mix fluent mappings with automappings? Ideally, I'd like to say "if I don't have a ClassMap for a domain object, then automap it". Is there a recommended approach? I'd rather not use attributes on my business objects that are data access related (ex: [UseAutoMapping]).
Yes - check out IAutoMappingOverride
Basically, any mappings which override the Automapping behaviour need to implement this interface.
e.g.
public class MyClassMap : IAutoMappingOverride<MyClass>
{
public void Override(AutoMapping<MyClass> mapping)
{
mapping.IgnoreProperty(host => host.HostName);
mapping.Table("BobsHardware");
}
}

Fluent nHibernate Auto Mapping - Issue with AutoMapping Override

I've just tried to get a project up and running with Fluent Automapping (I'm familiar with Fluent but used to write each of the maps)
I have an object ScriptType which has a ParseRules property
public class ScriptType : EntityBase
{
public virtual string Name { get; set; }
public virtual IList<ParseRule> ParseRules { get; set; }
}
This is being Auto Mapped as HasMany and I wanted References.
I therefore added an AutoMapping override to another assembly ...
public class ScriptTypeOverride : IAutoMappingOverride<ScriptType>
{
public void Override(AutoMapping<ScriptType> mapping)
{
mapping.References(x => x.ParseRules);
}
}
And altered my configuration as so ...
return configuration
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<DatabaseInfo>()
.IgnoreBase<EntityBase>()
.Conventions.AddFromAssemblyOf<KeyConvention>()
.UseOverridesFromAssemblyOf<ScriptTypeOverride>()));
But I get this .... :(
An association from the table ScriptType refers to an unmapped class: System.Collections.Generic.IList`1[[GIT.ScriptWizard.Entities.ParseRule ...
Can anyone help please?
References is for creating many-to-one
relationships between two entities,
and is applied on the "many side."
You're referencing a single other
entity, so you use the References
method. HasMany is the "other side" of
the References relationship, and gets
applied on the "one side."
From Fluent's website.
How should your relation work? It looks like a classic one ScriptType-to-many ParseRules, so this should be HasMany on ScriptType's side, as Fluent does.
Maybe, if you want to have bidirectional relationship here, where ParseRule's side is the "owning" side of the relation, you should use Inverse() in ScriptType.ParseRules mapping override.

Fluent NHibernate: mapping one-to-one

NHibernate seems to support a special case of one to one mapping (That doesn't require a 1-m mapping on each side of the relationship).
See this article by Ayende:
http://nhibernate.info/blog/2009/04/18/nhibernate-mapping-lt-one-to-one-gt.html
I have no idea how to specify this in Fluent NHibernate though - is this possible?
Ah, just found from a helpful person in the Fluent group that I can use
HasOne(x => x.Cover);
Missed it somehow before :/
One scenario is with subclasses. You can specific a table per hierarchy or per class.
You would need to override for the per hierarchy something like below:
public class UserMap : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
mapping.DiscriminateSubClassesOnColumn<int>("UserType");
}
}
public void Override(AutoMapping<Person> mapping)
{
mapping.Table("Persons");
DiscriminatorValue((int)UserTypes.Person);
}