I had an issue today where the SchemaExport was adding an extra fk
column in a ManyToMany lookup table.
I've narrowed it down to be reproduced (sort of, the example creates
an extra table) with the following short snippet of code (inserted
below at bottom). After many hours of learning/debugging the Fluent
NHibernate internals I've found the area which is causing the unwanted
behaviour.
In the RelationshipPairingVisitor class there's a FindAlternative
method which I believe isn't taking care in finding an alternative
that also has the same child type.
When I change this line
.Where(x => x.ContainingEntityType == current.ContainingEntityType
To this
.Where(x => x.ContainingEntityType == current.ContainingEntityType &&
x.ChildType == current.ChildType)
My problem goes away.
Is it me, am I doing something wrong?
Is it a bug, should I submit a patch?
-- Thanks Pleb
class Program
{
private const string ConnectionString =
"Host=mysql1.local.test.server;User
Id=Aieg;Password=Aieg;Database=AiegTest";
static void Main()
{
using (var connection = new MySqlConnection(ConnectionString))
{
connection.Open();
using (var command = new MySqlCommand())
{
command.Connection = connection;
command.CommandText = "DROP DATABASE AiegTest;";
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE AiegTest";
command.ExecuteNonQuery();
}
}
var sessionFactory = CreateSessionFactory();
using (var connection = new MySqlConnection(ConnectionString))
{
connection.Open();
using (var command = new MySqlCommand("SHOW TABLES;",
connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
}
}
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(ConnectionString))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
new SchemaExport(config)
.Create(false, true);
}
}
public class ProductMap : ClassMap
{
public ProductMap()
{
Id(x => x.Id)
.GeneratedBy.GuidComb();
HasManyToMany(x => x.Retailers)
.Access.BackingField()
.Cascade.SaveUpdate()
.AsSet();
HasManyToMany(x => x.Regions)
.Access.BackingField()
.Cascade.SaveUpdate()
.AsSet();
}
}
public class RegionMap : ClassMap
{
public RegionMap()
{
Id(x => x.Id)
.GeneratedBy.GuidComb();
HasManyToMany(x => x.Products)
.Access.BackingField()
.Inverse()
.AsSet();
}
}
public class RetailerMap : ClassMap
{
public RetailerMap()
{
Id(x => x.Id)
.GeneratedBy.GuidComb();
HasManyToMany(x => x.Products)
.Access.BackingField()
.Inverse()
.AsSet();
}
}
public class Product
{
public virtual Guid Id { get; protected set; }
public virtual Iesi.Collections.Generic.ISet Retailers
{ get; private set; }
public virtual Iesi.Collections.Generic.ISet Regions
{ get; private set; }
public Product()
{
Retailers = new
Iesi.Collections.Generic.HashedSet();
Regions = new Iesi.Collections.Generic.HashedSet();
}
}
public class Retailer
{
public virtual Guid Id { get; protected set; }
public virtual Iesi.Collections.Generic.ISet Products
{ get; set; }
public Retailer()
{
Products = new Iesi.Collections.Generic.HashedSet();
}
}
public class Region
{
public virtual Guid Id { get; protected set; }
public virtual Iesi.Collections.Generic.ISet Products
{ get; set; }
public Region()
{
Products = new Iesi.Collections.Generic.HashedSet();
}
}
--
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
To unsubscribe from this group, send email to
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en.