I have the following classes and mappings:
public enum VariationType
{
Base = 1,
RiderMain = 2,
RiderSpouse = 3,
RiderChild = 4,
Family = 5,
FamilyBase = 6
}
public class PlanParameter
{
private IDictionary<VariationType, PlanParameterDefaultValue> _defaultValues;
public PlanParameter()
{
ParameterContext = new Parameter();
}
public virtual Parameter ParameterContext { get; set; }
public virtual object DefaultValue { get; set; }
public virtual string DefaultValueString
{
get
{
return DefaultValue == null ? null : DefaultValue.ToString();
}
set
{
DefaultValue = value == null ? null : Convert.ChangeType(value, ParameterContext.Type);
}
}
public virtual IDictionary<VariationType, PlanParameterDefaultValue> DefaultValues
{
get
{
if (_defaultValues == null)
_defaultValues = new Dictionary<VariationType, PlanParameterDefaultValue>();
return _defaultValues;
}
}
}
class PlanParameterMap : ClassMap<PlanParameter>
{
public PlanParameterMap()
{
Id().GeneratedBy.Identity().Column("ID");
References(x => x.ParameterContext,"ParameterID");
Map(x => x.DefaultValueString);
HasMany(x=> x.DefaultValues)
.Access.CamelCaseField(Prefix.Underscore)
.KeyColumn("PlanParameterID").Inverse()
.AsMap("Variation")
.Cascade.AllDeleteOrphan();
}
}
public class PlanParameterDefaultValue
{
public virtual PlanParameter PlanParameter { get; set; }
public virtual object DefaultValue { get; set; }
public virtual string DefaultValueString
{
get
{
return DefaultValue == null ? null : DefaultValue.ToString();
}
set
{
DefaultValue = value == null ? null : Convert.ChangeType(value, PlanParameter.ParameterContext.Type);
}
}
}
class PlanParameterDefaultValueMap : ClassMap<PlanParameterDefaultValue>
{
public PlanParameterDefaultValueMap()
{
Id().GeneratedBy.Identity().Column("ID");
Map(x => x.DefaultValueString);
References(x => x.PlanParameter).Column("PlanParameterID");
}
}
My problem is very specific to the mapping of the
IDictionary<VariationType, PlanParameterDefaultValue> DefaultValues
The enum for some reason will not save, all that's saved in it's column is null
my only solution so far was to add a VariationType Property to the Entity and map it with a lambda formula, but i really don't need the VariationType in the Entity
Am i doing something wrong?
Thanks very much
The Inverse() tells NHibernate that the "mapped-to"-entity will take care of saving the key, and that the collection owner should not persist the Key. Try removing that flag.
Related
The project that I am working on at the moment is using Entity Framework, however there are some issues that we have come across and therefore I am researching using NHibernate which we believe will sort out the majority of issues we have.
Anyway, I have been replicating a simple part of the system, but I have ran into what I assume is a very simple problem with a one-to-many relationship as it is giving very strange results.
Here are my entities:
public class Task : Base.Domain
{
private IList<TaskProperty> _taskProperties = new BindingList<taskProperty>();
private string _name = String.Empty;
private string _description = String.Empty;
public virtual IList<TaskProperty> TaskProperties
{
get
{
return _taskProperties;
}
set
{
if (_taskProperties == value) return;
_taskProperties = value;
OnNotifiyPropertyChanged("TaskProperties");
}
}
public virtual string Name
{
get
{
return _name;
}
set
{
if (_name == value) return;
_name = value;
base.OnNotifiyPropertyChanged("Name");
}
}
public virtual string Description
{
get
{
return _description;
}
set
{
if (_description == value) return;
_description = value;
base.OnNotifiyPropertyChanged("Description");
}
}
public Task()
: base()
{ }
}
public class TaskProperty : Base.Domain
{
private Task _task = null;
private string _name = string.Empty;
private string _description = string.Empty;
private int _propertyType = 0;
//public virtual int TaskID { get; set; }
public virtual Task Task
{
get
{
return _task;
}
set
{
if (_task == value) return;
_task = value;
OnNotifiyPropertyChanged("Task");
}
}
public virtual string Name
{
get
{
return _name;
}
set
{
if (_name == value) return;
_name = value;
OnNotifiyPropertyChanged("Name");
}
}
public virtual string Description
{
get
{
return _description;
}
set
{
if (_description == value) return;
_description = value;
OnNotifiyPropertyChanged("Description");
}
}
public virtual int PropertyType
{
get
{
return _propertyType;
}
set
{
if (_propertyType == value) return;
_propertyType = value;
OnNotifiyPropertyChanged("PropertyType");
}
}
public TaskProperty()
: base()
{ }
}
Here are my NHibernate mappings:
public class TaskMapping : ClassMap<Task>
{
public TaskMapping()
{
Id(x => x.Id).Column("RETTaskID");
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.Version);
HasMany(x => x.TaskProperties).KeyColumn("RETTaskPropertyID");
Table("RETTask");
}
}
public class TaskPropertyMapping : ClassMap<TaskProperty>
{
public TaskPropertyMapping()
{
Id(x => x.Id).Column("RETTaskPropertyID");
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.PropertyType);
References(x => x.Task).Column("RETTaskID");
Table("RETTaskProperty");
}
}
Note: The Domain class which these entities inherit from holds the ID (int Id).
The problem that I am facing is that when I get I Task from the database with an ID of 27 for example, I get the TaskProperty with an ID of 27 as well, not the expected 4 TaskProperties that are related to the Task via a foreign key.
This worked fine in Entity Framework and I know this is a simple situation for any ORM, so I assume I have set up my mappings incorrectly, but from all the examples I have found, I don't seem to be doing anything wrong!
Any answers/suggestions will be most welcome. Thanks.
You are almost there. The Column mapping for HasMany and References must be the same:
public TaskMapping()
{
...
HasMany(x => x.TaskProperties).KeyColumn("RETTaskID"); // use this
// HasMany(x => x.TaskProperties).KeyColumn("RETTaskPropertyID"); // instead of this
}
public TaskPropertyMapping()
{
...
References(x => x.Task).Column("RETTaskID");
}
The collection item has to have a reference column to the owner. This column is used for both directions, because that's how the reference in DB managed...
I have a table mapping with nullable FK constraint. In my fluent mapping I am doing something like so:
public enum PlayerPosition
{
None = 0,
Forward = 1
//etc
}
Entity
public virtual PlayerPosition? Position { get; set; }
Map(x => x.Position).Column("PlayerPositionId").CustomType< PlayerPosition>();
What I would like to happen is when PlayerPosition is set to "None" Nhibernate will insert null. I am not sure how to make that happen.
i would go for IUserType:
public virtual PlayerPosition Position { get; set; }
Map(x => x.Position).Column("PlayerPositionId").CustomType<PlayerPositionUserType>();
class PlayerPositionUserType : IUserType
{
public object NullSafeGet(IDBReader reader, string[] names, object owner)
{
int? positionvalue = NHibernateUtil.Int32.NullSafeGet(reader, names[0]);
return (positionvalue.HasValue) ? (PlayerPosition)positionvalue : PlayerPosition.None;
}
public void NullSafeSet(IDBCommand cmd, object value, int index)
{
var position = (PlayerPosition)value;
if (position == PlayerPosition.None)
NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
else
NHibernateUtil.Int32.NullSafeSet(cmd, (int)position, index);
}
public Type ReturnType
{
get { return typeof(PlayerPosition); }
}
public SqlType[] SqlTypes
{
get { return new [] { SqlTypeFactory.Int32 } }
}
}
I'm trying to create a discriminator column. This column would hold one of the many statuses available. Like my code will show, each status has a name as well as a background color. Each status shares the same base class.
Here is my code:
public class Item
{
public virtual int Id { get; set; }
public virtual Status ItemStatus { get; set; }
}
public abstract class Status
{
private readonly int _id;
public static readonly Status Foo = new FooStatus(1);
public static readonly Status Bar = new BarStatus(2);
public Status()
{
}
protected Status(int id)
{
_id = id;
}
public virtual int Id { get { return _id; } }
public abstract string Name { get; }
public abstract string BackgroundColor { get; }
}
public class FooStatus : Status
{
public FooStatus()
{
}
public FooStatus(int id)
: base(id)
{
}
public override string Name
{
get { return "Foo Status"; }
}
public override string BackgroundColor
{
get { return "White"; }
}
}
public class BarStatus : Status
{
public BarStatus()
{
}
public BarStatus(int id)
: base(id)
{
}
public override string Name
{
get { return "Bar Status"; }
}
public override string BackgroundColor
{
get { return "Black"; }
}
}
And here is my mapping:
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
Id(x => x.Id).GeneratedBy.Identity();
DiscriminateSubClassesOnColumn<int>("ItemStatus", 0).AlwaysSelectWithValue();
}
}
Essentially, what I'd like is that if I set ItemStatus to Status.Foo then the ItemStatus column would have a value of 1. What I have now doesn't throw any exceptions, but it always inserts ItemStatus as 0.
This is the inserting code I'm using:
using (var session = sessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
var item = new Item
{
ItemStatus = Status.Foo
};
session.Save(item);
transaction.Commit();
var firstItem = session.Get<Item>(1);
Console.WriteLine(firstItem.ItemStatus.Name);
}
Where can I read up on this topic using FNH?
Before anyone suggests be to check on Google I did search several things but nowhere can I find a full example.
Your SubclassMap would look something like this:
public class FooStatusMap : SubclassMap<FooStatus>
{
public FooStatusMap()
{
DiscriminatorValue(1);
}
}
This is called "table-per-class-hierarchy," and you're right it doesn't look like there are many resources on it out there.
I believe if you don't call DiscriminatorValue in a SubclassMap, NHibernate attempts to discriminate by looking at the name of the subclass being mapped and seeing if it matches up with the value in the discriminator column.
I wouldnt write submaps for all the subclasses you can just do this instead
public class FooMap: ClassMap<T>
{
//other mapping
DiscriminateSubClassesOnColumn("DiscriminatorColumn")
.SubClass<Foo1>(m => { })
.SubClass<Foo2>(m => { })
.SubClass<Foo3>(m => { });
}
Hope that helps
If you're open to the Discriminator column having the class names of the derived classes, you can implement this via automapping.
In your session factory:
private static ISessionFactory CreateSessionFactory()
{
var cfg = new MyMappingConfiguration();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("MyConnectionKey")).FormatSql().ShowSql()
)
.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Status>(cfg)
.IncludeBase<Status>()
.Conventions.Add<PrimaryKeyConvention>()))
.BuildSessionFactory();
}
Then add the MyMappingConfiguration override:
public class MappingConfiguration : DefaultAutomappingConfiguration
{
public override bool IsId(Member member)
{
return member.Name == member.DeclaringType.Name + "Id";
}
public override bool IsDiscriminated(Type type)
{
return true;
}
}
Hope that h
This is very similar to my previous question: FluentNHibernate: How to translate HasMany(x => x.Addresses).KeyColumn("PersonId") into automapping
Say I have these models:
public class Person
{
public virtual int Id { get; private set; }
public virtual ICollection<Address> Addresses { get; private set; }
}
public class Address
{
public virtual int Id { get; private set; }
public virtual Person Owner { get; set; }
}
I want FluentNHibernate to create the following tables:
Person
PersonId
Address
AddressId
OwnerId
This can be easily achieved by using fluent mapping:
public class PersonMapping : ClassMap<Person>
{
public PersonMapping()
{
Id(x => x.Id).Column("PersonId");
HasMany(x => x.Addresses).KeyColumn("OwnerId");
}
}
public class AddressMapping : ClassMap<Address>
{
public AddressMapping()
{
Id(x => x.Id).Column("AddressId");
References(x => x.Person).Column("OwnerId");
}
}
I want to get the same result by using auto mapping. I tried the following conventions:
class PrimaryKeyNameConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.Column(instance.EntityType.Name + "Id");
}
}
class ReferenceNameConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
instance.Column(string.Format("{0}Id", instance.Name));
}
}
// Copied from #Fourth: https://stackoverflow.com/questions/6091290/fluentnhibernate-how-to-translate-hasmanyx-x-addresses-keycolumnpersonid/6091307#6091307
public class SimpleForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if(property == null)
return type.Name + "Id";
return property.Name + "Id";
}
}
But it created the following tables:
Person
PersonId
Address
AddressId
OwnerId
PersonId // this column should not exist
So I added a AutoMappingOverride:
public class PersonMappingOverride : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.HasMany(x => x.Addresses).KeyColumn("OwnerId");
}
}
This correctly solved the problem. But I want to get the same result using attribute & convention. I tried:
public class Person
{
public virtual int Id { get; private set; }
[KeyColumn("OwnerId")]
public virtual ICollection<Address> Addresses { get; private set; }
}
class KeyColumnAttribute : Attribute
{
public readonly string Name;
public KeyColumnAttribute(string name)
{
Name = name;
}
}
class KeyColumnConvention: IHasManyConvention
{
public void Apply(IOneToManyCollectionInstance instance)
{
var keyColumnAttribute = (KeyColumnAttribute)Attribute.GetCustomAttribute(instance.Member, typeof(KeyColumnAttribute));
if (keyColumnAttribute != null)
{
instance.Key.Column(keyColumnAttribute.Name);
}
}
}
But it created these tables:
Person
PersonId
Address
AddressId
OwnerId
PersonId // this column should not exist
Below is the rest of my code:
ISessionFactory sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString))
.Mappings(m =>
m.AutoMappings.Add(AutoMap.Assemblies(typeof(Person).Assembly)
.Conventions.Add(typeof(PrimaryKeyNameConvention))
.Conventions.Add(typeof(PrimaryKeyNameConvention))
.Conventions.Add(typeof(ReferenceNameConvention))
.Conventions.Add(typeof(SimpleForeignKeyConvention))
.Conventions.Add(typeof(KeyColumnConvention)))
//m.FluentMappings
// .Add(typeof (PersonMapping))
// .Add(typeof (AddressMapping))
)
.ExposeConfiguration(BuildSchema)
.BuildConfiguration()
.BuildSessionFactory();
Any ideas? Thanks.
Update:
The test project can be downloaded from here.
Sigh... Learning NHibernate is really a hair pulling experience.
Anyway I think I finally figured out how to solve this problem: Just remove the SimpleForeignKeyConvention and everything will work fine.
It seems the SimpleForeignKeyConvention conflicts with both ReferenceKeyConvention & KeyColumnConvention. It has higher priority than KeyColumnConvention but lower priority than ReferenceKeyConvention.
public class SimpleForeignKeyConvention : ForeignKeyConvention
{
protected override string GetKeyName(Member property, Type type)
{
if(property == null)
// This line will disable `KeyColumnConvention`
return type.Name + "Id";
// This line has no effect when `ReferenceKeyConvention` is enabled.
return property.Name + "Id";
}
}
I've tested your classes with FHN's auto-mapping feature and it does not create that second PersonId on Address table.
I'm using FHN v1.2.0.721 from here
I am having an issue with using Fluent NHibernate automapping with Inheritance. Below is my entity setup (abbreviated for simplicity). I have configured Fluent NHibernate to create 1 class for the hierarchy with a discriminator column. The automapping appears to be working correctly as when I generate a database, one table is created named "AddressBase" with a discriminator column that signals what type of address each row is.
The problem lies in the face that when I call the method "GetPrimaryBillingAddress()" on the UserAccount class, instead of just querying Billing Addresses, NHibernate is creating a query that looks at both Billing and Shipping Addresses. It doesn't take into account the discriminator at all. I am assuming there is some sort of configuration I can set but have not been able to find anything.
public abstract class AddressBase : ActiveRecord<AddressBase>
{
public virtual long Id { get; set; }
public virtual string Address1 { get; set; }
}
public class AddressBilling : AddressBase
{
public class TypedQuery : ActiveRecordQuery<AddressBilling> { }
public virtual bool IsPrimary { get; set; }
}
public class AddressShipping : AddressBase
{
public class TypedQuery : ActiveRecordQuery<AddressShipping> { }
[Display(Name = "Is Primary")]
public virtual bool IsPrimary { get; set; }
}
public class UserAccount : ActiveRecord<UserAccount>
{
public virtual long Id { get; set; }
public virtual IList<AddressBilling> BillingAddresses { get; set; }
public virtual IList<AddressShipping> ShippingAddresses { get; set; }
public UserAccount()
{
BillingAddresses = new List<AddressBilling>();
ShippingAddresses = new List<AddressShipping>();
}
public virtual AddressBilling GetPrimaryBillingAddress()
{
if (BillingAddresses.Any(x => x.IsPrimary))
{
return BillingAddresses.Single(x => x.IsPrimary);
}
return BillingAddresses.FirstOrDefault();
}
public virtual AddressShipping GetPrimaryShippingAddress()
{
if (ShippingAddresses.Any(x => x.IsPrimary)) {
return ShippingAddresses.Single(x => x.IsPrimary);
}
return ShippingAddresses.FirstOrDefault();
}
}
UPDATE:
Here is the Mapping override functions used in the automapping:
private static FluentConfiguration GetFluentConfiguration(string connectionStringName = "CS")
{
var autoMapping = AutoMap
.AssemblyOf<Product>(new Mapping.AutoMappingConfiguration())
.Conventions.Setup(c =>
{
c.Add<Mapping.ForeignKeyConvention>();
c.Add<Mapping.DiscriminatorConvention>();
})
.IgnoreBase<AddressBilling.TypedQuery>()
.IgnoreBase<AddressShipping.TypedQuery>()
.IncludeBase<AddressBase>();
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(connectionStringName)))
.Mappings(m => m.AutoMappings.Add(autoMapping));
}
public class AutoMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
var isStatic = type.IsAbstract && type.IsSealed;
return type.Namespace == typeof(Entities.Product).Namespace && !isStatic;
}
public override bool IsDiscriminated(Type type)
{
if (type == (typeof(Entities.AddressBase))) {
return true;
}
return false;
}
public override string GetDiscriminatorColumn(Type type)
{
return "Type";
}
public class DiscriminatorConvention : ISubclassConvention
{
public void Apply(ISubclassInstance instance)
{
//Address
if (instance.Name == typeof(AddressBilling).AssemblyQualifiedName)
{
instance.DiscriminatorValue(Enums.AddressType.BillingAddress);
}
else if (instance.Name == typeof(AddressShipping).AssemblyQualifiedName)
{
instance.DiscriminatorValue(Enums.AddressType.ShippingAddress);
}
}
}
Thanks!
Please, try to change your class UserAccount like this:
public class UserAccount : ActiveRecord<UserAccount>
{
public virtual IList<AddressBase> Addresses { get; set; }
public virtual IList<AddressBilling> BillingAddresses { get {return this.Addresses.OfType<AddressBilling>();} }
public virtual IList<AddressShipping> ShippingAddresses { get {return this.Addresses.OfType<AddressShipping>();} }
// ...
}
Of course, only Addresses property should be mapped here.