Replies: 1 comment 1 reply
-
You could try something like:
Don't forget to override Equals and GetHashCode if you use CompositeId(). |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to setup a one-to-many relation between 2 classes. These classes both have a composite Id, that is the same except for one column. But I get an error because my Id's have the same name.
This topic actually describes my problem very well, but the answer is useless to me because I can not change the name of the column in the database.
here are the classes:
public class Parent
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public List Children { get; set; }
public string Data { get; set; }
}
public class Child
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public Parent Parent { get; set; }
public string Data { get; set; }
}
public class ParentMap : ClassMap
{
public ParentMap()
{
CompositeId()
.KeyProperty(x => x.A)
.KeyProperty(x => x.B)
.KeyProperty(x => x.C);
Map(x => x.Data);
HasMany(x => x.Children)
.KeyColumns.Add("A", "B", "C");
}
}
public class ChildMap : ClassMap
{
public ChildMap()
{
CompositeId()
.KeyProperty(x => x.A)
.KeyProperty(x => x.B)
.KeyProperty(x => x.C)
.KeyProperty(x => x.D);
Map(x => x.Data);
References(x => x.Parent)
.Columns("A", "B", "C");
// Also tried adding this
// .Not.Insert()
// .Not.Update()
}
}
When I try to run this code the following exception pops up: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
With this inner exception: Unable to build the insert statement for class Parent: a failure occured when adding the Id of the class
This inner exception has another inner exception: The column 'A' has already been added in this SQL builder Parameternaam: columnName
I have seen in the fluent-nhibernate source code that it indeed adds no information about the table the column belongs to in the list of columns in the insert statement constructor. Is this a bug or can this be achieved another way? Preferably without changes to the database.
Beta Was this translation helpful? Give feedback.
All reactions