在引用上设置 CustomSqlType
我有一种情况,我的主键是 SqlServer 2008 中的 char(2),我想以一对多关系引用它,但是 ManyToOneBuilder (由 ClassMap<>.References() 返回)没有 CustomSqlType() 方法。具体来说:
public class State
{
// state FIPS code is 2 characters
public virtual string StateCode { get; set; }
public virtual ICollection<County> { get; set; }
}
public class County
{
// state-county FIPS code is 5 characters
public virtual string StateCountyCode { get; set; }
public virtual State State { get; set; }
}
public class StateMap : ClassMap<State>
{
public StateMap()
{
Id(e => e.StateCode).CustomSqlType("char(2)").GeneratedBy.Assigned();
}
}
public class CountyMap : ClassMap<County>
{
public CountyMap()
{
Id(e => e.StateCountyCode).CustomSqlType("char(5)").GeneratedBy.Assigned();
References(e => e.State, "StateCode")
// Here's what I want to do, but can't because the method is not
// implemented on the class ManyToOneBuilder:
.CustomSqlType("char(2)");
}
}
有没有什么方法可以在不修改 ManyToOneBuilder 的情况下完成此任务?有没有办法自动将 FK(即 County.StateCode)映射到正确的类型?将 CustomSqlType 添加到 ManyToOneBuilder 很简单,但这样做正确吗?
I have a situation where my primary key is a char(2) in SqlServer 2008, and I want to reference it in a one-to-many relationship, but the ManyToOneBuilder (which is returned by ClassMap<>.References()) doesn't have a CustomSqlType() method. Specifically:
public class State
{
// state FIPS code is 2 characters
public virtual string StateCode { get; set; }
public virtual ICollection<County> { get; set; }
}
public class County
{
// state-county FIPS code is 5 characters
public virtual string StateCountyCode { get; set; }
public virtual State State { get; set; }
}
public class StateMap : ClassMap<State>
{
public StateMap()
{
Id(e => e.StateCode).CustomSqlType("char(2)").GeneratedBy.Assigned();
}
}
public class CountyMap : ClassMap<County>
{
public CountyMap()
{
Id(e => e.StateCountyCode).CustomSqlType("char(5)").GeneratedBy.Assigned();
References(e => e.State, "StateCode")
// Here's what I want to do, but can't because the method is not
// implemented on the class ManyToOneBuilder:
.CustomSqlType("char(2)");
}
}
Is there any way to accomplish this without modifying the ManyToOneBuilder? Is there a way to automatically map the FK (i.e. County.StateCode) to the correct type? It's trivial to add CustomSqlType to ManyToOneBuilder, but is that the right thing to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保持映射定义不变,添加“State”列定义
并为此列设置 Insert=false 和 update=false。
我有同样的问题,在 AutoMapping 中我使用以下代码:
Keep your mapping definition as is, add your "State" column definition with
and set for this column Insert=false and update=false.
I've the same problem and in AutoMapping I use this code:
请记住,如果 NHibernate 本身不支持它,那么 Fluent NHibernate 就不能,而且我不 NHibernate 支持您所拥有的场景。我遇到了类似的问题,因为我在表和其中一个字段上有一个 2 列复合键,我想使用具有自定义 IUserType 的枚举类型将其转换为数据库中适当的代码值。无法做到这一点,所以我坚持保留字符串类型而不是枚举类型的属性。
Keep in mind that if NHibernate itself doesn't support it, then Fluent NHibernate can't, and I don't NHibernate supports the scenario you have. I had a similar problem in that I had a 2 column composite key on a table and on one of the fields, I wanted to use an enumerated type which had a custom IUserType to translate it to its appropriate code value in the DB. Couldn't do it, so I was stuck keeping the property of the string type rather than the enumerated type.