流利的NHibernate生成额外的列 [英] Fluent NHibernate generates extra columns
问题描述
几天前,我们遇到了一个问题,即Fluent NHibernate会生成一个额外的列,这个列在模型和映射中都不存在。这是情况:
我的模型:FirstClass.cs
public class FirstClass
{
public virtual int Id {get;私人设置}
public virtual SecondClass MyReference {get;组; }
public virtual DateTime DecisionDate {get;组; }
$ / code>
我的贴图:
public class FirstClassMap:ClassMap< FirstClass>
{
public FirstClassMap()
{
Id(x => x.Id);
Map(x => x.DecisionDate);
参考文献(x => x.MyReference);
$ b $ p $使用以下代码构建模式之后, ($)
$ b $ pre $ Instance._sessionFactory = Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString(connectionString)
.ShowSql())
.ExposeConfiguration(c =>
{
c.Properties.Add(current_session_context_class,ConfigurationHelper.getSetting(SessionContext)); $ ();
。)
.ExposeConfiguration(BuildSchema)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf< Community>))
.BuildSessionFactory
名为SecondClass_id的额外列是使用Id列的SecondClass表的索引和外键生成的。下面是生成的表格:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' ,
`DecisionDate` datetime DEFAULT NULL,
`MyReference_id` int(11)DEFAULT NULL,
`SecondClass_id` int(11)DEFAULT NULL,
PRIMARY KEY ),
KEY`MyReference_id`(`MyReference_id`),
KEY`SecondClass_id`(`SecondClass_id`),
CONSTRAINT`FK4AFFB59B2540756F` FOREIGN KEY(`MyReference_id`)REFERENCES`SecondClass` ``Id`),
CONSTRAINT`FK4AFFB59B51EFB484` FOREIGN KEY(`SecondClass_id`)REFERENCES`SecondClass`(`Id`),
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
我发现如果将MyReference重命名为SecondClass ),没有创建额外的列。但是我想用我指定的名字而不是类名来使用我的属性。为什么额外的列被创建?我如何解决这个问题?我不希望额外的外键列悬空。
当您使用FNH并且您拥有实体之间的双向关系。
public class FirstClass
{
public virtual SecondClass MyReference {get;组; }
}
public class SecondClass
{
public virtual List< FirstClass> ListOfFirstClass {get;组; }
}
public class FirstClassMap:ClassMap< FirstClass>
{
public FirstClassMap()
{
References(x => x.MyReference);
}
}
public class SecondClassMap:ClassMap< SecondClass>
{
Public SecondClassMap()
{
HasMany(x => x.ListOfFirstClass);
为了解决这个问题,你必须覆盖使用的列名在任何一个ClassMap中,如下所示:
public class SecondClassMap:ClassMap< SecondClass>
{
Public SecondClasssMap()
{
HasMany(x => x.ListOfFirstClass).KeyColumn(MyReference_id);
code
$ b public class FirstClassMap:ClassMap< FirstClass>
{
public FirstClassMap()
{
References(x => x.MyReference).Column(SecondClass_id);
$ p
$ b 原因是FNH将每个映射视为一个单独的关系,因此不同的列,键和索引被创建。
We are using Fluent NHibernate for data object model in the company i work.
A couple of days ago, we encountered an issue that Fluent NHibernate generates an extra column which does exist neither in model nor in mapping. Here is the situation:
My Model: FirstClass.cs
public class FirstClass
{
public virtual int Id { get; private set; }
public virtual SecondClass MyReference { get; set; }
public virtual DateTime DecisionDate { get; set; }
}
My Mapping:
public class FirstClassMap : ClassMap<FirstClass>
{
public FirstClassMap()
{
Id(x => x.Id);
Map(x => x.DecisionDate);
References(x => x.MyReference);
}
}
After building the schema with the following code,
Instance._sessionFactory = Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString(connectionString)
.ShowSql())
.ExposeConfiguration(c =>
{
c.Properties.Add("current_session_context_class", ConfigurationHelper.getSetting("SessionContext"));
})
.ExposeConfiguration(BuildSchema)
.Mappings( m => m.FluentMappings.AddFromAssemblyOf<Community>())
.BuildSessionFactory();
An extra column named "SecondClass_id" is produced with index and foreign key to SecondClass table with Id column. Here is the table produced:
CREATE TABLE `FirstClass` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`DecisionDate` datetime DEFAULT NULL,
`MyReference_id` int(11) DEFAULT NULL,
`SecondClass_id` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `MyReference_id` (`MyReference_id`),
KEY `SecondClass_id` (`SecondClass_id`),
CONSTRAINT `FK4AFFB59B2540756F` FOREIGN KEY (`MyReference_id`) REFERENCES `SecondClass` (`Id`),
CONSTRAINT `FK4AFFB59B51EFB484` FOREIGN KEY (`SecondClass_id`) REFERENCES `SecondClass` (`Id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I found that, if I rename "MyReference" to "SecondClass" (same name as the class type), there is no extra column created. But i want to use my property with the name i specified, not with the class name. Why that extra column is created? How do i fix that? I don't want extra foreign key columns hanging around.
解决方案 This often happens when you're using FNH and you have a two-way relationship between entities.
public class FirstClass
{
public virtual SecondClass MyReference { get; set; }
}
public class SecondClass
{
public virtual List<FirstClass> ListOfFirstClass { get; set; }
}
public class FirstClassMap : ClassMap<FirstClass>
{
public FirstClassMap()
{
References(x => x.MyReference);
}
}
public class SecondClassMap : ClassMap<SecondClass>
{
public SecondClassMap()
{
HasMany(x => x.ListOfFirstClass);
}
}
To fix this you have to override the column name used in either ClassMap, like so:
public class SecondClassMap : ClassMap<SecondClass>
{
public SecondClasssMap()
{
HasMany(x => x.ListOfFirstClass).KeyColumn("MyReference_id");
}
}
or:
public class FirstClassMap : ClassMap<FirstClass>
{
public FirstClassMap()
{
References(x => x.MyReference).Column("SecondClass_id");
}
}
The reason for this is that FNH treats each mapping as a separate relationship, hence different columns, keys, and indexes get created.
这篇关于流利的NHibernate生成额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!