NHibernate Domain Object Spans Multiple Tables

前端 未结 2 1528
轮回少年
轮回少年 2021-01-16 16:35

Assume I have this domain object...

public class SpansMultipleTables
{
     public int CommonID {get; set;}

     public string Table1Value {get; set;}

             


        
相关标签:
2条回答
  • 2021-01-16 17:16

    I'm not an expert, but I'm not sure if NHibernate can easily handle a mapping like this. If you are able to modify the schema, you might be able to define a "master" table, which just has the CommonID as the primary key. The class for the master table can then map the other tables as properties by their relationship to the master table. Another option might be to designate one of the tables as the master, and have the others mapped as properties on the master class. In any case, it would probably be a good idea to create some FK relationships between these tables to guarantee that the combined "entity" cannot have its parts removed separately.

    0 讨论(0)
  • 2021-01-16 17:26

    Try Join, but I would recommend changing your design.

    public class SpansMultipleTablesMap : ClassMap<SpansMultipleTables>
    {
      public SpansMultipleTablesMap()
      {
        Id(x => x.CommonID);
        Join("Table1", m =>
        {
          m.Map(x => x.Table1Value, "Value");
        });
        Join("Table2", m =>
        {
          m.Map(x => x.Table2Value, "Value");
        });
        Join("Table3", m =>
        {
          m.Map(x => x.Table3Value, "Value");
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题