Fluent NHibernate - 使用 2 列映射一对多

发布于 2024-12-27 01:35:43 字数 1789 浏览 2 评论 0原文

我有一个数据库架构,大致如下:

Product

  • ID
  • ProductName
  • Description
  • StoreBrand

ProductVariation

  • VariationID
  • ProductID
  • Size
  • StoreBrand
  • Price

Classes,可以预见,看起来有点像这样:

public class Product
{
  public virtual int ID { get; set; }
  public virtual string ProductName { get; set; }
  public virtual string Description { get; set; }
  public virtual string StoreBrand { get; set; }

  public virtual IEnumerable<ProductVariation> Variations { get; set; }
}

public class ProductVariation
{
  public virtual int VariationID { get; set; }
  public virtual int ProductID { get; set; }

  public virtual Product Product {get; set;}      

  public virtual string Size { get; set; }
  public virtual double Price { get; set; }
}

我有像这样映射类:

public class ProductMapper : ClassMap<Product>
{
  public ProductMapper()
  {
    Id(x => x.ID);
    Map(x => x.ProductName);
    Map(x => x.Description);
    Map(x => x.StoreBrand);

    HasMany(x => x.Variations)
      .KeyColumn("ProductID");
  }
}

public class ProductVariationMapper : ClassMap<ProductVariation>
{
  public ProductVariation()
  {
    Id(x => x.ID);
    Map(x => x.ProductID);
    Map(x => x.Size);
    Map(x => x.Price);

    References(x => x.Product)
      .Column("ProductID");
  }
}

有点工作...

但是,我需要做的是将 Product.Brands 与 ProductVariation.Brands 也绑定在一起...(反之亦然)

所以查询产品,返回该品牌的 ProductVariations 列表... (请注意,ProductVariation 在类中没有属性,但它具有用于映射的列)

ProductVariation.ID 不是唯一的。 关键是 ProductVariation.ID 和 ProductVariation.Brand (在数据库上)

I have a db schema along the lines of:

Product

  • ID
  • ProductName
  • Description
  • StoreBrand

ProductVariation

  • VariationID
  • ProductID
  • Size
  • StoreBrand
  • Price

Classes, predictably, look a bit like this:

public class Product
{
  public virtual int ID { get; set; }
  public virtual string ProductName { get; set; }
  public virtual string Description { get; set; }
  public virtual string StoreBrand { get; set; }

  public virtual IEnumerable<ProductVariation> Variations { get; set; }
}

public class ProductVariation
{
  public virtual int VariationID { get; set; }
  public virtual int ProductID { get; set; }

  public virtual Product Product {get; set;}      

  public virtual string Size { get; set; }
  public virtual double Price { get; set; }
}

I've got the mapping classes like this:

public class ProductMapper : ClassMap<Product>
{
  public ProductMapper()
  {
    Id(x => x.ID);
    Map(x => x.ProductName);
    Map(x => x.Description);
    Map(x => x.StoreBrand);

    HasMany(x => x.Variations)
      .KeyColumn("ProductID");
  }
}

public class ProductVariationMapper : ClassMap<ProductVariation>
{
  public ProductVariation()
  {
    Id(x => x.ID);
    Map(x => x.ProductID);
    Map(x => x.Size);
    Map(x => x.Price);

    References(x => x.Product)
      .Column("ProductID");
  }
}

This is kind of working...

However, what I need to do is tie the Product.Brands together with the ProductVariation.Brands as well... (and vice versa)

So querying Product, returns a list of it's ProductVariations for that brand...
(Notice, ProductVariation doesn't have a property in the class, but it has the column for mapping)

ProductVariation.ID is non unique.
The key is ProductVariation.ID and ProductVariation.Brand (on the database)

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

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

发布评论

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

评论(1

王权女流氓 2025-01-03 01:35:43
public class Product
{
    public virtual int ID { get; set; }
    public virtual string StoreBrand { get; set; }

    public virtual string ProductName { get; set; }
    public virtual string Description { get; set; }

    public virtual IEnumerable<ProductVariation> Variations { get; set; }

    public override Equals(object obj)
    {
        return Equals(obj as Product)
    }

    public override Equals(Product other)
    {
        return (other != null) && (Id == other.Id) && (StoreBrand == other.StoreBrand);
    }

    public override GetHashCode()
    {
        unchecked
        {
            return Id.GetHashCode() * 397 + StoreBrand.GetHashCode();
        }
    }
}

public class ProductVariation
{
    public virtual int ID { get; set; }

    public virtual Product Product {get; set;}      

    public virtual string Size { get; set; }
    public virtual double Price { get; set; }
}

public class ProductMapper : ClassMap<Product>
{
    public ProductMapper()
    {
        // Id alone is not unique, hence compositeId
        CompositeId()
            .KeyProperty(x => x.ID)
            .KeyProperty(x => x.StoreBrand);

        Map(x => x.ProductName);
        Map(x => x.Description);

        HasMany(x => x.Variations)
            .KeyColumn("ProductID", "StoreBrand");
    }
}

public class ProductVariationMapper : ClassMap<ProductVariation>
{
    public ProductVariation()
    {
        Id(x => x.ID);

        Map(x => x.Size);
        Map(x => x.Price);

        References(x => x.Product)
            .Column("ProductID", "StoreBrand");
    }
}
public class Product
{
    public virtual int ID { get; set; }
    public virtual string StoreBrand { get; set; }

    public virtual string ProductName { get; set; }
    public virtual string Description { get; set; }

    public virtual IEnumerable<ProductVariation> Variations { get; set; }

    public override Equals(object obj)
    {
        return Equals(obj as Product)
    }

    public override Equals(Product other)
    {
        return (other != null) && (Id == other.Id) && (StoreBrand == other.StoreBrand);
    }

    public override GetHashCode()
    {
        unchecked
        {
            return Id.GetHashCode() * 397 + StoreBrand.GetHashCode();
        }
    }
}

public class ProductVariation
{
    public virtual int ID { get; set; }

    public virtual Product Product {get; set;}      

    public virtual string Size { get; set; }
    public virtual double Price { get; set; }
}

public class ProductMapper : ClassMap<Product>
{
    public ProductMapper()
    {
        // Id alone is not unique, hence compositeId
        CompositeId()
            .KeyProperty(x => x.ID)
            .KeyProperty(x => x.StoreBrand);

        Map(x => x.ProductName);
        Map(x => x.Description);

        HasMany(x => x.Variations)
            .KeyColumn("ProductID", "StoreBrand");
    }
}

public class ProductVariationMapper : ClassMap<ProductVariation>
{
    public ProductVariation()
    {
        Id(x => x.ID);

        Map(x => x.Size);
        Map(x => x.Price);

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