Nhibernate数据绑定外键来lookupedit

时间:2012-11-03 13:27:37

标签: nhibernate data-binding devexpress

我想将外键属性Product.CategoryId数据绑定到Windows窗体应用程序中的Devexpess Lookupedit。 所以

        lookEditCategory.DataBindings
       .Add(new Binding("EditValue", Product, "CategoryId ", true,
        DataSourceUpdateMode.OnPropertyChanged));

        lookEditCategory.Properties.Columns.Clear();
        lookEditCategory.Properties.NullText = "";
        lookEditCategory.Properties.DataSource = CatCol;
        lookEditCategory.Properties.ValueMember = "CategoryId";
        lookEditCategory.Properties.DisplayMember = "CategoryName";
        var col = new LookUpColumnInfo("CategoryName") { Caption = "Type" };
        lookEditCategory.Properties.Columns.Add(col);

问题是Nhibernate不公开外键Product.CategoryId。相反,我的实体和映射就像这样

public partial class Product
{
public virtual int ProductId { get; set; }
[NotNull]
[Length(Max=40)]
public virtual string ProductName { get; set; }
public virtual bool Discontinued { get; set; }
public virtual System.Nullable<int> SupplierId { get; set; }

[Length(Max=20)]
public virtual string QuantityPerUnit { get; set; }
public virtual System.Nullable<decimal> UnitPrice { get; set; }
public virtual System.Nullable<short> UnitsInStock { get; set; }
public virtual System.Nullable<short> UnitsOnOrder { get; set; }
public virtual System.Nullable<short> ReorderLevel { get; set; }

private IList<OrderDetail> _orderDetails = new List<OrderDetail>();

public virtual IList<OrderDetail> OrderDetails
{
  get { return _orderDetails; }
  set { _orderDetails = value; }
}

public virtual Category Category { get; set; }

public class ProductMap : FluentNHibernate.Mapping.ClassMap<Product>
{
  public ProductMap()
  {
    Table("`Products`");
    Id(x => x.ProductId, "`ProductID`")
      .GeneratedBy
        .Identity();
    Map(x => x.ProductName, "`ProductName`")
;
    Map(x => x.Discontinued, "`Discontinued`")
;
    Map(x => x.SupplierId, "`SupplierID`")
;

    Map(x => x.QuantityPerUnit, "`QuantityPerUnit`")
;
    Map(x => x.UnitPrice, "`UnitPrice`")
;
    Map(x => x.UnitsInStock, "`UnitsInStock`")
;
    Map(x => x.UnitsOnOrder, "`UnitsOnOrder`")
;
    Map(x => x.ReorderLevel, "`ReorderLevel`")
;
    HasMany(x => x.OrderDetails)
      .KeyColumn("`ProductID`")
      .AsBag()
      .Inverse()
      .Cascade.None()
;
    References(x => x.Category)
      .Column("`CategoryID`");
  }
}
}

我无法在我的Product实体和映射中添加属性CategoryID,因为它将被映射两次。 有没有解决方案?

1 个答案:

答案 0 :(得分:0)

是。不要在用户界面中使用您的域名实体 有时您的UI不需要(也不应该知道)域对象的所有属性 其他时候,它需要包含来自不同域源的数据的DTO(例如 - CourseName屏幕的Student列表),或者,就像您的情况一样 - 它需要表示数据以稍微不同的方式 因此,最好的方法是使用UI所需的所有(且仅限)属性来创建DTO 有关详细信息,请参阅this SO question