【问题标题】:Nhibernate databinding foreign key to lookupeditNhibernate 数据绑定外键查找编辑
【发布时间】:2012-10-23 23:05:36
【问题描述】:

我想将外键属性 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`");
  }
}
}

我无法在我的产品实体和映射中添加属性 CategoryID,因为那样它将被映射两次。 有什么解决办法吗?

【问题讨论】:

    标签: nhibernate data-binding devexpress


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 2012-08-05
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多