.net Nhib根据类型将一列Map到两个表

6mw9ycah  于 2023-02-20  发布在  .NET
关注(0)|答案(1)|浏览(97)

是否可以为此SQL结构创建休眠Map(因此,基于CompanyType,它将CompanyId链接到CompanyTypeA或CompanyTypeB):

----
UserTable
----
Id (int)
CompanyId (int)
CompanyType (int)
...
----

----
CompanyTypeA
----
Id (int)
Name (varchar)
----

----
CompanyTypeB
----
Id (int)
Name (varchar)
----

 public class User
 {
    public virtual int Id { get; set; }
    public virtual int CompanyId { get; set; }
 }

公司类型A.Id和公司类型.Id彼此唯一。
我感兴趣的是c#代码Map而不是xml。谢谢。

8yparm6h

8yparm6h1#

public class User : Entity
{
    public virtual ICompany Company { get; set; }
}

public interface ICompany
{
    int Id { get; }
}

public class CompanyA : Entity, ICompany { }

public class CompanyB : Entity, ICompany { }

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(u => u.Id);

        ReferencesAny(u => u.Company)
            .IdentityType<int>()
            .EntityIdentifierColumn("CompanyId")
            .EntityTypeColumn("CompanyType")
            .AddMetaValue<CompanyA>("companyA")
            .AddMetaValue<CompanyB>("companyB");
    }
}
public class CompanyAMap : ClassMap<CompanyA>
{
    public CompanyAMap()
    {
        Id(u => u.Id);
    }
}
public class CompanyBMap : ClassMap<CompanyB>
{
    public CompanyBMap()
    {
        Id(u => u.Id);
    }
}

相关问题