在引用上设置CustomSqlType

在引用上设置CustomSqlType

本文介绍了在引用上设置CustomSqlType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的情况,我的主键是SqlServer 2008中的char(2),我想以一对多关系引用它,但是ManyToOneBuilder(由ClassMap返回.)> .References( ))没有CustomSqlType()方法.具体来说:

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)");
    }
}

是否有任何方法可以完成而无需修改ManyToOneBuilder?有没有一种方法可以自动将FK(即County.StateCode)映射到正确的类型?将CustomSqlType添加到ManyToOneBuilder很简单,但这是正确的做法吗?

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?

推荐答案

保持映射定义不变,使用

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

.CustomSqlType("char(2)")

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

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");

这篇关于在引用上设置CustomSqlType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:51