8

FluentNhibernateを使用して単純な.NETプロジェクトを開始しています。
私はインターネットで見つけたいくつかの例をたどりましたが、それは非常に理解しやすいようです。
FluentNhibernateにDBスキーマ(SQL Server 2000)を構築させると、文字列モデルプロパティのNVARCHARフィールドが生成されることに気付きました。

タイプを変更するための規則を追加できると誰かが提案しました。

このコードはうまく機能します:

public class AnsiStringConvention : IPropertyConvention 
        {
            private static Type stringType = typeof(string);
            public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
            {
                if (instance.Property.PropertyType == stringType)
                {
                    instance.CustomType("AnsiString");
                }
            }
        }

予想どおり、DBフィールドはVARCHARになりました。
別のクラスにAddressを配置し、それをCustomerクラスに追加した、DDDパターンに従って、コンポーネントをクラスに追加する必要がありました。
アドレス用に別のマッピングファイルを作成しました:

public class AddressMap : ComponentMap<Address>
{
    public AddressMap()
    {
        Map(x => x.Number);
        Map(x => x.Street)
            .Length(100);
        Map(x => x.City)
            .Length(50);
        Map(x => x.PostCode)
            .Length(5);
    }
}

これで、CustomerテーブルのすべてのフィールドはVARCHARですが、Address(Street、City、PostCode)コンポーネントのフィールドは引き続きNVARCHARとして作成されます。

4

1 に答える 1

12

次のように定義AnsiStringされているソリューションが見つかりましたCustomType:

public class AddressMap : ComponentMap<Address>
{
    public AddressMap()
    {
        // this.Map(x => x.Number);
        this.Map(x => x.Street)
            .CustomType("AnsiString") 
            .Length(100);
        this.Map(x => x.City)
            .CustomType("AnsiString")
            .Length(30);
        this.Map(x => x.State)
            .CustomType("AnsiString")
            .Length(20);
        this.Map(x => x.PostalCode)
            .CustomType("AnsiString")
            .Length(10);
        this.Map(x => x.Country)
            .CustomType("AnsiString")
            .Length(40);
    }
}
于 2010-08-26T10:49:56.570 に答える