1

コードの下で、Customer は複数の住所を持つことができます。一対多の関係があります。「Customer_id」ではなく「Customer」という名前のフィールドを FK として Address テーブルに入れたい

追加しようとしました: .KeyColumn("Customer")>変更なし

私はForeignKeyConventionを変更せずに使用しようとしました。

何か案が ?

public class CustomerMap : ClassMap<Customer>
{
    protected CustomerMap()
    {
        Id(x => x.Id).Not.Nullable();
        Map(x => x.FirstName).Not.Nullable().Length(25);
        Map(x => x.LastName);
        HasMany<Address>(x => x.Addresses)                
            .KeyColumn("Customer")
            .AsSet()
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

public class AddressMap : ClassMap<Address>
{
    public AddressMap()
    {
        Id(x => x.Id);
        Map(x => x.City).Not.Nullable().Length(100);
    }
}

public class ForeignKeyReferenceConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.PropertyRef("EntityId");
    }
}

public void DBCreation()
{
    FluentConfiguration config = Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString("...."))
        .Mappings(m =>
            m.AutoMappings
                .Add(AutoMap.AssemblyOf<Customer>())
                .Add(AutoMap.AssemblyOf<Address>()
                    .Conventions.Setup(c => c.Add<ForeignKeyReferenceConvention>())
                    )
                );

    config.ExposeConfiguration(
              c => new SchemaExport(c).Execute(true, true, false))
         .BuildConfiguration();
}
4

1 に答える 1

0

自分で自動マッピングを使用したことはありませんがClassMap、「デフォルト」の流暢なマッピング(自動マッピングではない)でのみ使用されているのではありませんか?流暢なマッピングまたは自動マッピングを使用しますか?

多対1の側面がマッピングされていないのに、なぜ1対多の逆数なのですか?

ところで、その大会の目的は何ですか?PropertyRef()どうしても必要な場合を除いて、使用しないでください(NHibernateはそれを使用していくつかの最適化を行うことはできません)。

于 2011-10-18T07:13:08.773 に答える