Mapping a list of Enums - fluent-nhibernate

I have a table called UserPermissions with a FK to the users table by userId and then a string column for the string value of an enum.
The error I am seeing is NHibernate.MappingException: An association from the table UserPermissions refers to an unmapped class: GotRoleplay.Core.Domain.Model.Permission
My Permission Enum:
public enum Permission
{
[StringValue("Add User")]
AddUser,
[StringValue("Edit User")]
EditUser,
[StringValue("Delete User")]
DeleteUser,
[StringValue("Add Content")]
AddContent,
[StringValue("Edit Content")]
EditContent,
[StringValue("Delete Content")]
DeleteContent,
}
The property in my User class:
public virtual IList<Permission> Permissions { get; set; }
My database table:
CREATE TABLE dbo.UserPermissions
(
UserPermissionId int IDENTITY(1,1) NOT NULL,
UserId int NOT NULL,
PermissionName varchar (50) NOT NULL,
CONSTRAINT PK_UserPermissions PRIMARY KEY CLUSTERED (UserPermissionId),
CONSTRAINT FK_UserPermissions_Users FOREIGN KEY (UserId) REFERENCES Users(UserId),
CONSTRAINT U_User_Permission UNIQUE(UserId, PermissionName)
)
My attempt at mapping the permissions property of my user object:
HasManyToMany(x => x.Permissions)
.WithParentKeyColumn("UserId")
.WithChildKeyColumn("PermissionName")
.WithTableName("UserPermissions")
.LazyLoad();
What am I doing wrong that it can't map the permission to a list of enum values?

here is the way which worked for me
HasMany(x => x.Licences)
.WithTableName("DriverLicence")
.AsElement("Level").AsBag();
look here for more information answer

You need to specify the type so that NHibernate can convert the value in the table to a member of the Permission enum.
HasManyToMany(x => x.Permissions)
.WithParentKeyColumn("UserId")
.WithChildKeyColumn("PermissionName")
.WithTableName("UserPermissions")
.LazyLoad()
.CustomTypeIs(typeof(Permission));
Edited to add:
I'm sorry, I should have noticed that you had this as ManyToMany. That's not possible: You can't have a Users collection (other side of m:m) hanging off an enum. You need to define this as 1:m or create a Permission table and class and map that as m:m.

I thought I would post the code that I chose for my solution. It's only a workaround. I wish Fluent would support Enum lists, but until it does, here's a possible solution:
The Enum - This is my enum, your standard enum.
public enum PermissionCode
{
//site permissions 1-99
ViewUser = 1,
AddUser = 2,
EditUser = 3,
DeleteUser = 4
}
Next, I have my Permission class.
public class Permission
{
public virtual int PermissionId { get; set; }
public virtual string PermissionName { get; set; }
public virtual PermissionCode PermissionCode
{
get
{
return (PermissionCode)PermissionId;
}
}
}
As you can see, I have an ID and a name, and then a property that converts the Id into my PermissionCode enum.
The mapping looks like this:
public class PermissionMap : ClassMap<Permission>
{
public PermissionMap()
{
WithTable("Permissions");
Id(x => x.PermissionId).GeneratedBy.Identity();
Map(x => x.PermissionName);
}
}
Since the PermissionCode property is derived, we don't do anything in the mapping.
My Table structure behind the mapping looks like this:
CREATE TABLE dbo.Permissions
(
PermissionId int NOT NULL,
PermissionName varchar (50) NOT NULL,
CONSTRAINT PK_Permissions PRIMARY KEY CLUSTERED (PermissionId)
)
If you wanted to use the name instead of the integer value, with some slight modifications you can. It depends on your personal preferences.

Related

Fluent NHibernate mapping to return description from lookup table

We have the following database structure:
UserTeam table
Id (PK, int not null)
UserId (FK, int, not null)
TeamId (FK, int, not null)
RoleId (FK, int, not null)
libRole table
Id (PK, int not null)
Description (varchar(255), not null)
And we have an entity as follows:
public class UserTeam
{
public int Id { get; set; }
public Team Team { get; set; }
public User User { get; set; }
public int RoleId { get; set; }
public string Role { get; set; }
}
We are using Fluent NHibernate and configuring NHibernate automatically (ie, using Automapping classes with overrides).
We are trying to get JUST the description column from the libRole table into the "Role" property on the UserTeam table, but really struggling. The following is the closest we have got:
public class UserTeamMap : IAutoMappingOverride<UserTeam>
{
public void Override( FluentNHibernate.Automapping.AutoMapping<UserTeam> mapping )
{
mapping.References( m => m.User ).Column( "UserId" );
mapping.References( m => m.Team ).Column( "TeamId" );
mapping.Join("Role", join =>
{
join.Fetch.Join();
join.KeyColumn( "Id" );
join.Map( x => x.Role, "Description" );
} );
}
}
Which generates the following SQL:
SELECT
TOP (#p0) this_.Id as Id70_0_,
this_.RoleId as RoleId70_0_,
this_.TeamId as TeamId70_0_,
this_.UserId as UserId70_0_,
this_1_.Description as Descript2_71_0_
FROM
[UserTeam] this_
inner join
libRole this_1_
on this_.Id=this_1_.Id;
Close, but NHibernate is using the Id column on both the UserTeam table and the libRole table in the join, when it should be doing on this_.RoleId=this_1_.Id
What are we missing? We don't really want to create a "libRole" entity within the application, as all we really care about is the description values - which are user configurable, so we can't just use an enum either. Can anyone help?
Join uses the primary key of the parent table. Its not possible to change this to a foreign key. See the docs for further details on what is possible with Join.
In this situation I would recommend creating an entity for the lookup. But if you really want to take this approach you could map the property with a formula, i.e.
Map(x => x.Role).Formula("(select description from libRole where Id = RoleId)");
Note this isn't perfect because it uses RoleId so if the query has another table with a column named RoleId then the DBMS will complain when trying to execute the SQL.

Fluent NHibernate automapping composite id with component

I have this complex situation: a database of countries/regions/states/cities which primary key is composed by a code (nvarchar(3)) in a column called "Id" plus all key columns of "ancestors" (regions/states/cities).
So the table country has only one key coumn (Id) while cities has 4 key columns (Id, StateId,regionId,CountryId). Obviously they're all related, so each ancestor column is a foreign key to the related table.
I have Entities in my Model that map this relationships. But they all derive from one type called Entity<T> where T may be a simple type (string, in etc) or a complex one (a component implementing the key).
Entity<T> implements a single property called Id of type T.
For each db table, if it has a comlex key, I implement it in a separate component, which oveerides also Equals and GetHashCode() Methods (in future I'll implement those in the Entity base class).
So I have a RegionKey componet that has 2 properties (Id and CountryId).
I have conventions for Foreign Key and primary key naming and type and that is ok.
I have also Mapping ovverrides for each complex Entity.
For simplicity, lets concentrate only on Countries and Regions table. Here they are:
public class Country: Entity<string>
{
public virtual string Name { get; set; }
public virtual IList<Region> Regions { get; set; }
}
public class Region: Entity<RegionKey>
{
public virtual string Name { get; set; }
public virtual Country Country { get; set; }
}
and the RegionKey component:
namespace Hell.RealHellState.Api.Entities.Keys
{
[Serializable]
public class RegionKey
{
public virtual string Id { get; set; }
public virtual string CountryId { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as RegionKey;
if (t == null)
return false;
return Id == t.Id && CountryId == t.CountryId;
}
public override int GetHashCode()
{
return (Id + "|" + CountryId).GetHashCode();
}
}
}
Here is the configuration of AutoPersistenceModel:
public ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
MsSqlCeConfiguration.Standard
.ConnectionString(x=>x.Is(_connectionString))
)
.Mappings(m => m.AutoMappings.Add(AutoMappings))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private AutoPersistenceModel AutoMappings()
{
return AutoMap.Assembly(typeof (Country).Assembly)
.IgnoreBase(typeof(Entity<>))
.Conventions.AddFromAssemblyOf<DataFacility>()
.UseOverridesFromAssembly(GetType().Assembly)
.Where(type => type.Namespace.EndsWith("Entities"));
}
private static void BuildSchema(Configuration config)
{
//Creates database structure
new SchemaExport(config).Create(false, true);
//new SchemaUpdate(config).Execute(false, true);
}
Here is the Regions entity overrides
public class RegionMappingOverride : IAutoMappingOverride<Region>
{
public void Override(AutoMapping<Region> mapping)
{
mapping.CompositeId(x=>x.Id)
.KeyProperty(x => x.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
.KeyProperty(x => x.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
}
}
Ok now when I test this mapping I got an error saying: The data types of the columns in the relationship do not match.
I have also tried this override:
public void Override(AutoMapping<Region> mapping)
{
mapping.CompositeId()
.ComponentCompositeIdentifier(x=>x.Id)
.KeyProperty(x => x.Id.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
.KeyProperty(x => x.Id.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
}
And it almost work but it creates a Regions table with a single column key of varbinary(8000) which is not what I want:
CREATE TABLE [hell_Regions] (
[Id] varbinary(8000) NOT NULL
, [Name] nvarchar(50) NULL
, [CountryId] nvarchar(3) NULL
);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [PK__hell_Regions__0000000000000153] PRIMARY KEY ([Id]);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [FK_Regions_Country] FOREIGN KEY ([CountryId]) REFERENCES [hell_Countries]([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
I don't have a clue of how to deal with it since it seems to me everythin is ok.
Thanks in advance for your answers
Ok I menaged to solve it: I had to sign the CompositeId class as MAPPED, since it is a component. So this is my new RegionMappingOverride:
public class RegionMappingOverride : IAutoMappingOverride<Region>
{
public void Override(AutoMapping<Region> mapping)
{
mapping.CompositeId(x=>x.Id)
.Mapped()
.KeyProperty(x =>x.Id,x=>x.Length(3))
.KeyProperty(x => x.CountryId, x=>x.Length(3));
}
}
Now the sql created is correct:
create table hell_Countries (
Id NVARCHAR(3) not null,
Name NVARCHAR(50) null,
primary key (Id)
)
create table hell_Regions (
Id NVARCHAR(3) not null,
CountryId NVARCHAR(3) not null,
Name NVARCHAR(50) null,
primary key (Id, CountryId)
)
alter table hell_Regions
add constraint FK_Region_Country
foreign key (CountryId)
references hell_Countries

Mapping self-referencing IDictionary<string, Entity> with Fluent NHibernate

I have the following entities in my domain model:
class Entity
{
public int Id { get; set; }
}
class Foo : Entity
{
public IDictionary<string, Attribute> Attributes { get; set; }
}
class Bar : Entity
{
public IDictionary<string, Attribute> Attributes { get; set; }
}
class Attribute : Entity
{
public string Key { get; set; }
public string Value { get; set; }
public IDictionary<string, Attribute> Attributes { get; set; }
}
I'd like to map these dictionaries with Fluent NHibernate. I've gotten most things to work, but first I'm having difficulties with the self-referencing Attribute.Attributes property. This is due to NHibernate making the Key a primary key of the Attribute table as well as the Id it inherits from Entity. This is how my mapping works:
ManyToManyPart<Attribute> manyToMany = mapping
.HasManyToMany<Attribute>(x => x.Attributes)
.ChildKeyColumn("AttributeId")
.ParentKeyColumn(String.Concat(entityName, "Id"))
.AsMap(x => x.Key, a => a.Column("`Key`"))
.Cascade.AllDeleteOrphan();
if (entityType == typeof(Attribute))
{
manyToMany
.Table("AttributeAttribute")
.ParentKeyColumn("ParentAttributeId");
}
If I replace the if statement with the following:
if (entityType == typeof(Attribute))
{
manyToMany
.Table("Attribute")
.ParentKeyColumn("ParentAttributeId");
}
I get the following exception:
NHibernate.FKUnmatchingColumnsException : Foreign key (FK_Attribute_Attribute [ParentAttributeId])) must have same number of columns as the referenced primary key (Attribute [ParentAttributeId, Key])
This is due to NHibernate automatically making Key the primary key alongside Id in my Attribute column. I'd like Key to not be primary key, since it shows up in all of my many to many tables;
create table FooAttribute (
FooId INT not null,
AttributeId INT not null,
[Key] NVARCHAR(255) not null
)
I'd like the foreign keys to only reference Id and not (Id, Key), since having Key as a primary key requires it to be unique, which it won't be across all of my ManyToManys.
where do you map Attribute itself (does it contain a Composite Key)?
AttributeValue may be a better name to show that it contains a value.
.AsMap(x => x.Key) is enough to say that Key should be the dictionary key
create table FooAttribute (
FooId INT not null,
AttributeValueId INT not null
)
or consider using
mapping.HasMany(x => x.Attributes)
.KeyColumn(entity + Id)
.AsMap(x => x.Key)
.Cascade.AllDeleteOrphan();
which will create
create table Attribute (
Id INT not null,
FooId INT,
BarId INT,
ParentAttributeId INT,
Key TEXT,
Value TEXT,
)

FluentNHibernate Many-To-One References where Foreign Key is not to Primary Key and column names are different

I've been sitting here for an hour trying to figure this out...
I've got 2 tables (abbreviated):
CREATE TABLE TRUST
(
TRUSTID NUMBER NOT NULL,
ACCTNBR VARCHAR(25) NOT NULL
)
CONSTRAINT TRUST_PK PRIMARY KEY (TRUSTID)
CREATE TABLE ACCOUNTHISTORY
(
ID NUMBER NOT NULL,
ACCOUNTNUMBER VARCHAR(25) NOT NULL,
TRANSAMT NUMBER(38,2) NOT NULL
POSTINGDATE DATE NOT NULL
)
CONSTRAINT ACCOUNTHISTORY_PK PRIMARY KEY (ID)
I have 2 classes that essentially mirror these:
public class Trust
{
public virtual int Id {get; set;}
public virtual string AccountNumber { get; set; }
}
public class AccountHistory
{
public virtual int Id { get; set; }
public virtual Trust Trust {get; set;}
public virtual DateTime PostingDate { get; set; }
public virtual decimal IncomeAmount { get; set; }
}
How do I do the many-to-one mapping in FluentNHibernate to get the AccountHistory to have a Trust? Specifically, since it is related on a different column than the Trust primary key of TRUSTID and the column it is referencing is also named differently (ACCTNBR vs. ACCOUNTNUMBER)???? Here's what I have so far - how do I do the References on the AccountHistoryMap to Trust???
public class TrustMap : ClassMap<Trust>
{
public TrustMap()
{
Table("TRUST");
Id(x => x.Id).Column("TRUSTID");
Map(x => x.AccountNumber).Column("ACCTNBR");
}
}
public class AccountHistoryMap : ClassMap<AccountHistory>
{
public AccountHistoryMap()
{
Table("TRUSTACCTGHISTORY");
Id (x=>x.Id).Column("ID");
References<Trust>(x => x.Trust).Column("ACCOUNTNUMBER").ForeignKey("ACCTNBR").Fetch.Join();
Map(x => x.PostingDate).Column("POSTINGDATE");
);
I've tried a few different variations of the above line but can't get anything to work - it pulls back AccountHistory data and a proxy for the Trust; however it says no Trust row with given identifier.
This has to be something simple. Anyone?
Thanks in advance.
You need to use property-ref:
public class AccountHistoryMap : ClassMap<AccountHistory>
{
public AccountHistoryMap()
{
Table("TRUSTACCTGHISTORY");
Id (x=>x.Id).Column("ID");
References(x => x.Trust, "ACCOUNTNUMBER").PropertyRef("ACCTNBR").Fetch.Join();
Map(x => x.PostingDate).Column("POSTINGDATE");
}
}

Specify a Fluent NHibernate automapping to add a unique constraint to all entities

My automapping:
return Fluently.Configure()
.Database(config)
.Mappings(m =>
m.AutoMappings.Add(
AutoMap.AssemblyOf<Company>()
.Where(
t => t.Namespace == "DAL.DomainModel" && t.IsClass)
.IgnoreBase<ReferenceEntity>()))
.BuildSessionFactory();
So ReferenceEntity is an abstract class containing a string Name, and all my reference entities inherit from this class. I'd like to modify my automapping to add a unique constraint to the Name field for all entities that inherit from ReferenceEntity.
I've gathered it has something to do with .Setup but I'm a bit lost on how to proceed.
note: I'm using the Fluent NHibernate v1.0 RTM so conventions will be with the new style if that is relavent to my goal.
If all your entities inherit from ReferenceEntity, wouldn't you want to create the unique constraint for the Name property on all the entities that are mapped?
But if you want to filter by entity base class, you can do it. Use a convention to add the unique constraint to your mappings:
public class NameConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
// Check the entity base class type
if (instance.EntityType.BaseType.Name == "ReferenceEntity")
{
// Only add constraint to the .Name property
if (instance.Name == "Name")
{
instance.Unique();
}
}
}
}
To get the convention (and all other conventions in the assembly) picked up by FNH, just add this line the AutoMap setup you have above:
.Conventions.AddFromAssemblyOf<NameConvention>()
Alex,
No the answer doesn't change. Here is an example, using the convention above.
public abstract class ReferenceEntity
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
}
public class User : ReferenceEntity
{
public virtual string Email { get; set; }
}
public class Item : ReferenceEntity
{
public virtual string Description { get; set; }
}
This creates sql of:
create table Users (
Id INTEGER not null,
Email TEXT not null,
Name TEXT not null unique,
primary key (Id)
)
create table Items (
Id INTEGER not null,
Description TEXT,
Name TEXT not null unique,
primary key (Id)
)
As long as these are separate entities, it will create a unique constraint on the .Name property for each entity.