在引用上设置 CustomSqlType

发布于 2024-10-10 15:38:19 字数 1205 浏览 2 评论 0原文

我有一种情况,我的主键是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

壹場煙雨 2024-10-17 15:38:19

保持映射定义不变,添加“State”列定义

.CustomSqlType("char(2)")

并为此列设置 Insert=false 和 update=false。

我有同样的问题,在 AutoMapping 中我使用以下代码:

mapping.Map(x => x.IdUniArticolo)
     .CustomSqlType("varchar(50)")
     .Not.Insert().Not.Update();

mapping.References(x => x.Articolo)
     .Column("IdUniArticolo").PropertyRef("IdUniArticolo");

Keep your mapping definition as is, add your "State" column definition with

.CustomSqlType("char(2)")

and set for this column Insert=false and update=false.

I've the same problem and in AutoMapping I use this code:

mapping.Map(x => x.IdUniArticolo)
     .CustomSqlType("varchar(50)")
     .Not.Insert().Not.Update();

mapping.References(x => x.Articolo)
     .Column("IdUniArticolo").PropertyRef("IdUniArticolo");
隐诗 2024-10-17 15:38:19

请记住,如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文