通过两个非主键列连接的流畅 Nhibernate 映射

发布于 2024-12-23 12:37:11 字数 1496 浏览 3 评论 0原文

我想知道是否有一种方法可以在不创建视图的情况下连接两个具有两个非主键列的表?我有一个名为“Make”的表,其中包含“Name”和“Year”列,我想将其与另一个名为“Style”的表连接,其中包含“MakeName”和“MakeYear”列。一种“品牌”可以有多种“风格”。 以下是我迄今为止创建的实体:

public class Make
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Year { get; set; }
    public virtual IList<Style> Styles { get; set; } 
}

public class Style
{
    public virtual int Id { get; set; }
    public virtual string MakeName { get; set; }
    public virtual string MakeYear { get; set; }
    public virtual string Class { get; set; }
    public virtual Make Make { get; set; }
}

另外,这些是我迄今为止拥有的类映射:

public class MakeMap : ClassMap<Make>
{
    public MakeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Year);
        // Bad mapping...
        //HasManyToMany(x => x.Styles).Table("Make").AsBag()
          .ParentKeyColumn("MakeName")
          .ChildKeyColumn("MakeYear").Cascade.All();
        Table("Make");
    }
}

public class StyleMap : ClassMap<Style>
{
    public StyleMap()
    {
        Id(x => x.Id);
        Map(x => x.Class);
        Map(x => x.MakeName);
        Map(x => x.MakeYear);
        // Ends up overwriting the "MakeName" column
        References(x => x.Make).Column("MakeName").PropertyRef("Name").
              Column("MakeYear").PropertyRef("Year");
        Table("Style");
    }
}

谢谢!

I was wondering is there a way to join two tables with two non primary key columns without creating a view? I have a table called 'Make' with columns of 'Name' and 'Year' that I want to join with another table called 'Style' with columns 'MakeName' and 'MakeYear'. One 'Make' can have many 'Style'.
Here are the entities that I have created so far:

public class Make
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Year { get; set; }
    public virtual IList<Style> Styles { get; set; } 
}

public class Style
{
    public virtual int Id { get; set; }
    public virtual string MakeName { get; set; }
    public virtual string MakeYear { get; set; }
    public virtual string Class { get; set; }
    public virtual Make Make { get; set; }
}

Also these are the class maps I have so far:

public class MakeMap : ClassMap<Make>
{
    public MakeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Year);
        // Bad mapping...
        //HasManyToMany(x => x.Styles).Table("Make").AsBag()
          .ParentKeyColumn("MakeName")
          .ChildKeyColumn("MakeYear").Cascade.All();
        Table("Make");
    }
}

public class StyleMap : ClassMap<Style>
{
    public StyleMap()
    {
        Id(x => x.Id);
        Map(x => x.Class);
        Map(x => x.MakeName);
        Map(x => x.MakeYear);
        // Ends up overwriting the "MakeName" column
        References(x => x.Make).Column("MakeName").PropertyRef("Name").
              Column("MakeYear").PropertyRef("Year");
        Table("Style");
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-12-30 12:37:11

3 个选项,我更喜欢第一个

  1. 选项:

    更改数据库架构以将make id而不是名称和年份包含到样式表中

  2. Option

References(x => x.Make)
    .Formula("(Select s.Id FROM Style s WHERE s.Name = MakeName AND s.Year = MakeYear)");
  1. 选项
public class Make
{
    public virtual int Id { get; set; }

    public virtual MakeId BusinessId { get; private set; }
}

public class MakeId
{
    public virtual string Name { get; set; }
    public virtual string Year { get; set; }
}

public class MakeMap : ClassMap<Make>
{
    public MakeMap()
    {
        Id(x => x.Id);
        Component("BusinessId", c => 
            c.Map(x => x.Name);
            c.Map(x => x.Year);
        });

        HasMany(x => x.Styles)
          .PropertyRef("BusinessId")
          .KeyColumns.Add("MakeName", "MakeYear")
          .Cascade.All();
        Table("Make");
    }
}

public class StyleMap : ClassMap<Style>
{
    public StyleMap()
    {
        Table("Style");

        Id(x => x.Id);
        Map(x => x.Class);

        References(x => x.Make)
            .PropertyRef("BusinessId")
            .Columns.Add("MakeName", "MakeYear");
    }
}

3 options where i would prefere the first

  1. Option:

    change the db schema to include the make id instead of the name and year into the style table

  2. Option

References(x => x.Make)
    .Formula("(Select s.Id FROM Style s WHERE s.Name = MakeName AND s.Year = MakeYear)");
  1. Option
public class Make
{
    public virtual int Id { get; set; }

    public virtual MakeId BusinessId { get; private set; }
}

public class MakeId
{
    public virtual string Name { get; set; }
    public virtual string Year { get; set; }
}

public class MakeMap : ClassMap<Make>
{
    public MakeMap()
    {
        Id(x => x.Id);
        Component("BusinessId", c => 
            c.Map(x => x.Name);
            c.Map(x => x.Year);
        });

        HasMany(x => x.Styles)
          .PropertyRef("BusinessId")
          .KeyColumns.Add("MakeName", "MakeYear")
          .Cascade.All();
        Table("Make");
    }
}

public class StyleMap : ClassMap<Style>
{
    public StyleMap()
    {
        Table("Style");

        Id(x => x.Id);
        Map(x => x.Class);

        References(x => x.Make)
            .PropertyRef("BusinessId")
            .Columns.Add("MakeName", "MakeYear");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文