【问题标题】:Creating a new class based on existing class基于现有类创建新类
【发布时间】:2012-04-09 22:56:06
【问题描述】:

我将我的问题引用到 Northwind LINQ to SQL C# 示例(取自 MSDN):

数据库包含一个名为Products 的表,其中有一个名为CategoryID 的字段。 因此,NorthWindDataClasses.designer.cs 文件包含一个名为 Product 的类,该类具有以下数据成员:

private System.Nullable<int> _CategoryID;

还有一个名为 Categories 的表,其中包含以下两个字段:

  • CategoryID。
  • CategoryName.

我想创建一个新类(在我的 Accessor.cs 文件中),其中包含 Product 类中的所有字段,但将有 CategoryName 而不是 _CategoryID。

实现它的最佳方法是什么?

【问题讨论】:

  • 为什么?你不能通过 Product.Category.CategoryName 得到你想要的?
  • 生成的Product 类不包含公共Category 属性吗?

标签: c# linq linq-to-sql northwind


【解决方案1】:

最好的方法是将 CategoryName 属性添加到 Product 类(它是部分的并且允许您添加到它)。

public partial class Product
{
  public string CategoryName
  {
    get {return this.Category.CategoryName;}
    set{this.Category.CategoryName = value;}
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 2015-11-28
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    相关资源
    最近更新 更多