Fluent Nhibernate composed entity, specify foreign key - nhibernate

I have a Fluent Nhibernate map like :
public class UserMap : ClassMap<PortalUser>
{
public UserMap()
{
WithTable("aspnet_Users");
Id(x => x.Id, "UserId")
.GeneratedBy.Guid();
Map(x => x.Name, "UserName");
Map(x => x.Login, "LoweredUserName");
WithTable("LdapUsers", m => m.Map(x => x.FullName, "FullName"));
}
}
My foreign key column in table "LdapUser" is UserId but the select that gets generated is going to look for a "PortalUserId".
Is there a way to specify the relation key direcly?

Try this:
...
WithTable("LdapUsers", m => {
m.Map(x => x.FullName, "FullName");
m.WithKeyColumn("UserId");
});

Related

Sharing field as composite key and foreign key in Fluent NHibernate

Consider following mapping code please :
public sealed class BankMapping : ClassMap<Bank>
{
public BankMapping()
{
CompositeId()
.KeyProperty(x => x.SerialNumber, "SerialBankRef")
.KeyProperty(x => x.FinancialPeriodId, "FinancialPeriodRef");
Map(x => x.Code);
Map(x => x.Title);
Map(x => x.Comment);
Map(x => x.IsActive);
HasMany(x => x.BankBranchs).KeyColumns.Add(new[] { "SerialBankRef", "FinancialPeriodRef" });
}
}
And the BankBranch mapping code is :
public sealed class BankBranchMapping : ClassMap<BankBranch>
{
public BankBranchMapping()
{
CompositeId()
.KeyProperty(x => x.FinancialPeriodId, "FinancialPeriodRef")
.KeyProperty(x => x.SerialNumber, "SerialNumber");
Map(x => x.Code);
Map(x => x.Title);
Map(x => x.Comment);
Map(x => x.IsActive);
Map(x => x.Address);
Map(x => x.Fax);
Map(x => x.Telephone);
References(x => x.Bank).Columns(new[] { "SerialBankRef", "FinancialPeriodRef" });
}
}
As you can see the FinancialPeriodRef field acts as foreign key and part of composite key in the BankBranch mapping and NHibernate builds DB correctly and everything seems to be fine until I try to insert a record in the BankBranch table.
The error System.IndexOutOfRangeException : Invalid index 10 for this SqlParameterCollection with Count=10. which indicates I've mapped a field (FinancialPeriodRef) twice as FK and PK appears and I have no idea how to fix the problem.
I need FinancialPeriodRef in the BankBranch as part of primary key while it's absolutely equal to FinancialPeriodRef from Bank.
I need this field to establish unique constraint and also benefits of composite key is essential.
You need to use KeyReference rather than KeyProperty to describe composite foreign keys.
public BankBranchMapping()
{
CompositeId()
.KeyReference(x => x.FinancialPeriodId, "FinancialPeriodRef")
.KeyReference(x => x.SerialNumber, "SerialNumber");
...
}
I had the exact same problem as you and after an hour or so came across this post: https://stackoverflow.com/a/7997225/569662 which pointed me right.

Fluent nhibernate : Problems when deleting a HasOne relationship

I have two classes
public PartMap()
{
Id(x => x.ID).GeneratedBy.Guid();
HasOne(x => x.Stock)
.Cascade.All();
}
public Stock
{
Id(x => x.ID).GeneratedBy.Guid();
References(x => x.Part);
}
Part has only one stock, that's the reason i used HasOne.
The data is inserted ok, I have a part , a stock and they are saved ok.
My problem is when trying to delete I get an error that the stock foreing key to part was violated
"ORA-02292: integrity constraint (PRINERGY.FK121AD9E59966BE23) violated " .
I see the it tries to delete the part without deleting the related stock before.
How can I solve it?
It seems like a one-to-one bidirectional association. There are two ways how to map this association in NH (and FNH): primary key association and foreign key association. Your example is closer to foreign key association.
Foreign key association
public PartMap()
{
Id(x => x.Id).GeneratedBy.Guid();
References(x => x.Stock).Unique().Cascade.All();
Map(x => x.Name);
}
public StockMap()
{
Id(x => x.Id).GeneratedBy.Guid();
HasOne(x => x.Part).Cascade.All().PropertyRef("Stock");
Map(x => x.Name);
}
Primary key association
public PartMap()
{
Id(x => x.Id).GeneratedBy.Guid();
HasOne(x => x.Stock).Cascade.All();
Map(x => x.Name);
}
public StockMap()
{
Id(x => x.Id).GeneratedBy.Foreign("Part");
HasOne(x => x.Part).Constrained();
Map(x => x.Name);
}
For more details see http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html.

Fluent NHibernate misplaced duplicate field

I think I'm misunderstanding something about how this works. This is my fluent mapping:
public class FunctionInfoMap : ClassMap<FunctionInfo>
{
public FunctionInfoMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Signature);
Map(x => x.IsNative);
Map(x => x.ClassId);
References(x => x.Class, "ClassId");
HasMany(x => x.CallsAsParent).Inverse();
HasMany(x => x.CallsAsChild).Inverse();
Table("Functions");
}
}
public class CallMap : ClassMap<Call>
{
public CallMap()
{
CompositeId()
.KeyProperty(x => x.ThreadId)
.KeyProperty(x => x.ParentId)
.KeyProperty(x => x.ChildId)
.Mapped();
Map(x => x.ThreadId).Index("Calls_ThreadIndex");
Map(x => x.ParentId).Index("Calls_ParentIndex");
Map(x => x.ChildId).Index("Calls_ChildIndex");
Map(x => x.HitCount);
References(x => x.Thread, "ThreadId");
References(x => x.Parent, "ParentId");
References(x => x.Child, "ChildId");
Table("Calls");
}
}
public class SampleMap : ClassMap<Sample>
{
public SampleMap()
{
CompositeId()
.KeyProperty(x => x.ThreadId)
.KeyProperty(x => x.FunctionId)
.Mapped();
Map(x => x.ThreadId);
Map(x => x.FunctionId);
Map(x => x.HitCount);
References(x => x.Thread, "ThreadId");
References(x => x.Function, "FunctionId");
Table("Samples");
}
}
Now when I create this schema into a fresh database, that FunctionId field from SampleMap winds up in the Calls table.
create table Calls (
ThreadId INTEGER not null,
ParentId INTEGER not null,
ChildId INTEGER not null,
HitCount INTEGER,
FunctionId INTEGER,
primary key (ThreadId, ParentId, ChildId)
)
create table Samples (
ThreadId INTEGER not null,
FunctionId INTEGER not null,
HitCount INTEGER,
FunctionId INTEGER,
primary key (ThreadId, FunctionId)
)
I don't understand why it's there, since it should only exist in the Samples table.
You should not map both the foreign key and the many-ton-one relationship. Instead of
Map(x => x.ThreadId).Index("Calls_ThreadIndex");
Map(x => x.ParentId).Index("Calls_ParentIndex");
Map(x => x.ChildId).Index("Calls_ChildIndex");
Map(x => x.HitCount);
References(x => x.Thread, "ThreadId");
References(x => x.Parent, "ParentId");
References(x => x.Child, "ChildId");
map the many-to-ones which use the foreign keys
Map(x => x.HitCount);
References(x => x.Thread, "ThreadId");
References(x => x.Parent, "ParentId");
References(x => x.Child, "ChildId");
I don't know if this will solve your problem. Also, I strongly advise against using composite keys. Replace them with surrogate key (identity) and unique indexes.
I finally figured out the problem, more or less. The pair of collections on FunctionInfoMap were confusing NHibernate, since they don't actually lead anywhere. Adding KeyColumn entries to link them up to the Parent and Child associations corrected the stray field.

Fluent NHibernate HasManyToMany() Save/Update Problem

i have the following code, which is supposed to give specific functionality but it isn't :S
anyway, here's my problem:
http://img525.imageshack.us/img525/1315/diagramp.png
here's the mapping code:
public class UsersMap : ClassMap<User>
{
public UsersMap()
{
this.Table("Users");
Id(x => x.UserName).GeneratedBy.Assigned();
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Password);
Map(x =>x.EMail);
Map(x => x.Title);
Map(x => x.Division);
Map(x => x.Status);
HasManyToMany(x => x.Roles)
.Table("UserInRoles").ParentKeyColumn("Username")
.ChildKeyColumn("RoleId").AsBag().Inverse();
}
}
public class RolesMap : ClassMap<Role>
{
public RolesMap()
{
this.Table("Roles");
Id(x => x.ID).GeneratedBy.Assigned();
Map(x => x.RoleName);
HasManyToMany(x => x.Users)
.Table("UserInRoles").ParentKeyColumn("RoleId")
.ChildKeyColumn("Username").AsBag().Cascade.All();
}
}
my problem is when trying to (assign a Role for specific user) the UserName is added to table UserInRoles but the Role ID if it is already existing it will be removed from its corrosponding row and assigned to the new row, any idea ?
Have you looked making your ManyToMany work by convention?
Linked example actually refers to Roles and pretty much what you are trying to do here.

Abort due to constraint violation columns GroupId, idx are not unique

I'm using FluentNHibernate and have done a many-to-many mapping but when I try to save my entity I get the following error:
NHibernate.Exceptions.GenericADOException: NHibernate.Exceptions.GenericADOException
: could not insert collection: [Test.Entities.Recipient.Groups#b6815d34-f436-4142-9b8e-1bfcbf25509e][SQL: SQL not available]
---- System.Data.SQLite.SQLiteException : Abort due to constraint violation
columns GroupId, idx are not unique
Here is my mapping:
public class GroupMap : ClassMap<Group>
{
public GroupMap()
{
Id(x => x.Id).GeneratedBy.Guid();
Map(x => x.Name);
Map(x => x.SenderName);
Map(x => x.Created);
HasManyToMany(x => x.Recipients)
.AsList()
.WithTableName("groups_recipients")
.WithParentKeyColumn("GroupId")
.WithChildKeyColumn("RecipientId")
.LazyLoad()
.Cascade.AllDeleteOrphan();
}
}
public class RecipientMap : ClassMap<Recipient>
{
public RecipientMap()
{
Id(x => x.Id).GeneratedBy.Guid();
Map(x => x.Firstname);
Map(x => x.Lastname);
Map(x => x.Phone);
Map(x => x.Email);
HasManyToMany(x => x.Groups)
.AsList()
.WithTableName("groups_recipients")
.WithParentKeyColumn("RecipientId")
.WithChildKeyColumn("GroupId")
.LazyLoad().Cascade.None();
}
}
The problem seems to have something to do with the relationship tables id but I can't figure out how to solve it.
Cheers,
nandarya
Using AsList() was not a good idea. Should be AsBag(). And everything seems to work.