简体   繁体   中英

Mapping a column to a table which has no foreign key constraint between them using fluent Nhibernate

I have 2 tables like this

CREATE TABLE [dbo].[TariffConfig]
(
     PKey INT IDENTITY NOT NULL,
     TariffDeterminatorCd INT NOT NULL
)

CREATE TABLE [dbo].[TariffDeterminator]
(
     PKey INT IDENTITY NOT NULL,
     Tariff INT NOT NULL
)

the TariffDeterminatorCd references a row (it is equal to PKey in TariffDeterminator) from TariffDeterminator but has no foreign key constraint between them.

and their entity classes are like this

public class TariffDeterminator : EntityBase
{       
    public virtual Int32 Age { get; set; }

    public virtual Int32 Tariff { get; set; }
}

public class TariffConfig : EntityBase
{
    public virtual TariffDeterminator Determinator{ get; set; }
}

I have a TariffDeterminator like this

public class TariffDeterminatorMap : EntityBaseMap<TariffDeterminator>
{       
    public TariffDeterminatorMap ()
        : base()
    {
        this.Initialize("TariffDeterminator");
        Map(x => x.Age).Not.Nullable();
        Map(x => x.Tariff).Not.Nullable();
    }
}

Can anyone help me to write Fluent Nhibernate mapping for TariffConfig entity ?

i never used FluentNHibernate but it should look like this

public class TariffConfigMap : EntityBaseMap<TariffConfig>
{

    public TariffConfigMap() : base()
    {
       this.Initialize("TariffConfig");
       References(prop => prop.Determinator)
            .ColumnName("TariffDeterminatorCd");           
    }

}

i can't see the primary key mapped in your entity classes, i assume these properties are inherited and mapped during Initialize().

You generally don't need a foreign key constraint to map relations between entities, you only need the foreign key itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM