首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nhibernate数据绑定外键到lookupedit

Nhibernate数据绑定外键到lookupedit
EN

Stack Overflow用户
提问于 2012-11-03 21:27:37
回答 1查看 313关注 0票数 2

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

代码语言:javascript
运行
复制
        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。相反,我的实体和映射如下所示

代码语言:javascript
运行
复制
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,因为它将被映射两次。有什么解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2012-11-06 04:48:03

是。不要在UI中使用域实体。

有时,您的UI不需要(也不应该知道)域对象的所有属性。

其他时候,它需要包含来自不同域源的数据的CourseName(例如,Student屏幕的DTO列表),或者,就像您的示例一样,它需要以稍微不同的方式表示数据。

因此,最好的方法是创建具有UI所需的所有(且仅有)属性的DTO。

有关更多详细信息,请参阅this SO question

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13209633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档