简体   繁体   中英

Mapping one entity to multiple tables

I have a question related to the Fluent NHibernate. I can not describe the schema mapping one entity to multiple tables. There is the following structure of the database:

Create table CeTypes (Id int not null PRIMARY KEY, Name nvarchar(100) not null)
Create table CeValues (Id int not null PRIMARY KEY, Name nvarchar(100) not null)
Create table Ces (Id int not null PRIMARY KEY, CeType_id int not null FOREIGN KEY REFERENCES CeTypes(Id), CeValue_id int not null FOREIGN KEY REFERENCES CeTypes(Id))

there is the following entity:

public class Ce
{
     public virtual int Id { get; set; }
     public virtual string Type { get; set; }
     public virtual string Value { get; set; }
}

CeType, CeValue entities in the domain and there is no. I do not know how to describe the mapping Ce entity.

Tried to describe:

public class CeMap : ClassMap<Ce>
{
    public CeMap()
    {
        Table("Ces");
        Id(c => c.Id);

        Join("CeTypes", m => m.Map(ce => ce.Type).Column("Name"));
        Join("CeValues", m => m.Map(ce => ce.Value).Column("Name"));
    }
}

But with such a scheme CeType, CeValue tables should have a field Ce_id. How can I describe scheme mapping under the current structure of the database?

I tried doing the same thing when I first started using nHibernate and couldn't find a way to do it. I actually don't believe that you can map multiple tables to a single object. Usually you would have one entity per table. Each entity will be mapped to their table and would have references/hasmany links between them.

You'll probably find that having one entity per table is better in the long run as well because it allows for simpler mapping to the database.

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