LINQ for Windows Phone Mango 应用程序中的外键关系

发布于 2024-12-28 19:42:08 字数 1505 浏览 4 评论 0原文

我刚刚开始为 WP (Mango) 编写应用程序,并且遇到了与如何使用 LINQ 映射查找表的外键关系相关的问题。 MSDN 文档似乎没有帮助(也许我找错了地方)。基本上,我有 2 个表,

OrderType(由 Orders 引用的查找数据组成)

Orders(这将有一个名为 OrderTypeID 的列指向上表)

基本上是一对多关系。

以下是我在 LINQ 中的描述:

[Table(Name = "OrderTypes")]
public partial class OrderType
{
    private Int16 _OrderTypeID;
    private string _Name;
    private string _ShortName;

    [Column(Storage = "_OrderTypeID", DbType = "Int NOT NULL", IsPrimaryKey = true, CanBeNull=false)]
    public Int16 Id {get; set;}

    [Column(Storage = "_Name", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
    public string Name {get;set;}

    [Column(Storage= "_ShortName", DbType= "NVarChar(50) NOT NULL", CanBeNull=false)]
    public string ShortName {get;set;}
}

[Table(Name = "Orders")]
public partial class Order
{
    private Int16 _OrderID;
    private Int16 _OrderTypeID;
    private string _Description;

    [Column(Storage = "_OrderID", DbType = "Int NOT NULL", IsPrimaryKey = true)]
    public Int16 Id {get;set;}

    [Column(DbType = "Int", CanBeNull= true)]
    public Int16 OrderTypeID { get; set; }

    [Column(DbType = "NVarChar(1000) NULL", CanBeNull = true)]
    public string Description { get; set; }

}

我对以下内容感到困惑:如何使用 EntityRef< /a> 和/或 EntitySet 类来描述我的代码中的这种关系。

提前致谢。

I'm just getting started writing an app for WP (Mango), and am running into issues related to how to map a foreign key relationship for a lookup table using LINQ. The MSDN docs didnt seem to help (maybe i'm looking in the wrong place). Basically, I have 2 tables,

OrderType (this consists of lookup data referenced by Orders)

Orders (this will have a column called OrderTypeID pointing back to the above table)

basically a 1-to-many relationship.

Here's how I've described in LINQ:

[Table(Name = "OrderTypes")]
public partial class OrderType
{
    private Int16 _OrderTypeID;
    private string _Name;
    private string _ShortName;

    [Column(Storage = "_OrderTypeID", DbType = "Int NOT NULL", IsPrimaryKey = true, CanBeNull=false)]
    public Int16 Id {get; set;}

    [Column(Storage = "_Name", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
    public string Name {get;set;}

    [Column(Storage= "_ShortName", DbType= "NVarChar(50) NOT NULL", CanBeNull=false)]
    public string ShortName {get;set;}
}

[Table(Name = "Orders")]
public partial class Order
{
    private Int16 _OrderID;
    private Int16 _OrderTypeID;
    private string _Description;

    [Column(Storage = "_OrderID", DbType = "Int NOT NULL", IsPrimaryKey = true)]
    public Int16 Id {get;set;}

    [Column(DbType = "Int", CanBeNull= true)]
    public Int16 OrderTypeID { get; set; }

    [Column(DbType = "NVarChar(1000) NULL", CanBeNull = true)]
    public string Description { get; set; }

}

I confused re: how to use the EntityRef and/or EntitySet classes to describe this relationship in my code.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2025-01-04 19:42:08
  • EntitySet 位于“1”侧,它包含“many”侧的实体。

  • EntityRef 位于“多”侧的实体上,引用“1”侧的(父)实体。

在您的情况下,订单将有一个 EntityRef。反过来,OrderType 可以有一个
EntitySet,尽管我认为后者没有多大意义(从语义上讲,人们可能不会说 OrderType“有”订单,就像 Order“有”订单行一样)。

在 Adventureworks 数据库中,当映射类似关系 Product 和 ProductCategory 时,Product 类具有 ProductCategory 属性:

private EntityRef<ProductCategory> _ProductCategory;

[System.Data.Linq.Mapping.AssociationAttribute(Storage="_ProductCategory",
    ThisKey="ProductCategoryID", OtherKey="ProductCategoryID", IsForeignKey=true)]
public ProductCategory ProductCategory
{
  get { return this._ProductCategory.Entity; }
  set { this._ProductCategory.Entity = value; }
}
  • EntitySet is on the "1" side, it contains the entities on the "many" side.

  • EntityRef is on an entity of the "many" side, referring to the (parent) entity on the "1" side.

In your case Order would have an EntityRef<OrderType>. OrderType, in turn, could have an
EntitySet<Order>, although I'd think the latter would not make much sense (semantically one would probably not say that an OrderType "has" orders, like an Order "has" order lines).

In the Adventureworks database, when mapping a similar relationship, Product and ProductCategory, the Product class has a ProductCategory property:

private EntityRef<ProductCategory> _ProductCategory;

[System.Data.Linq.Mapping.AssociationAttribute(Storage="_ProductCategory",
    ThisKey="ProductCategoryID", OtherKey="ProductCategoryID", IsForeignKey=true)]
public ProductCategory ProductCategory
{
  get { return this._ProductCategory.Entity; }
  set { this._ProductCategory.Entity = value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文