我想将外键属性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,因为它将被映射两次。有什么解决方案吗?
发布于 2012-11-06 04:48:03
是。不要在UI中使用域实体。
有时,您的UI不需要(也不应该知道)域对象的所有属性。
其他时候,它需要包含来自不同域源的数据的CourseName(例如,Student屏幕的DTO列表),或者,就像您的示例一样,它需要以稍微不同的方式表示数据。
因此,最好的方法是创建具有UI所需的所有(且仅有)属性的DTO。
有关更多详细信息,请参阅this SO question。
https://stackoverflow.com/questions/13209633
复制相似问题