English 中文(简体)
2. 液化NHibernate - 用2栏绘制一对一图
原标题:Fluent NHibernate - Mapping one-to-many using 2 columns

I have a db schema along the lines of:

<<>Product>

  • ID
  • ProductName
  • Description
  • StoreBrand

<><>ProductVariation>

  • VariationID
  • ProductID
  • Size
  • StoreBrand
  • Price

可以预测,这些等级就象这样:

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");
  }
}

页: 1

然而,我需要做的是将产品捆绑在一起。 Brands与产品Variation。 Brands 以及...(和反之亦然)

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)

问题回答
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");
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签