简体   繁体   English

通过外键关联两个以上的表

[英]Relating More than two tables through foreign keys

create table Customer
(
 ID int not null primary key,
 Name varchar(30) not null
)
create table Purchase
(
 ID int not null primary key,
 CustomerID int references Customer (ID),
 Description varchar(30) not null,
 Price decimal not null
)

First above is an sql script through my sql server management studio to create two tables (Customer and Purchase).Then the following classes is added to the C# code as follows. 上面的第一个是通过我的SQL Server Management Studio创建一个SQL脚本以创建两个表(Customer和Purchase)。然后将以下类添加到C#代码中,如下所示。

[Table(Name = "Customer")]
public class Customer
{
    [Column(IsPrimaryKey = true)]
    public int ID;
    [Column(Name = "Name")]
    public string Name;
}

[Table(Name = "Purchase")]
public class Purchase
{
    [Column(IsPrimaryKey = true)]
    public int ID;
    [Column]
    public int CustomerID;
    [Column]
    public string Descriptions;
    [Column]
    public decimal Price;
}

This is the main function. 这是主要功能。

DataContext dataContext = new DataContext(@"Server=.\SQLEXPRESS;Database=master;Trusted_Connection=True;");
Table<Customer> customers = dataContext.GetTable<Customer>();

foreach (Purchase p in customers.Purchases)
    Console.WriteLine(p.Price);

And it's giving me an error on the foreach statement. 这给了我关于foreach语句的错误。

Error 1 'System.Data.Linq.Table' does not contain a definition for 'Purchase' and no extension method 'Purchase' accepting a first argument of type 'System.Data.Linq.Table' could be found (are you missing a using directive or an assembly reference?) 错误1'System.Data.Linq.Table'不包含'Purchase'的定义,并且找不到扩展方法'Purchase'接受类型为'System.Data.Linq.Table'的第一个参数(您是否缺少使用指令还是程序集引用?)

You have no association defined on your LINQ Table definitions. 您在LINQ表定义中没有定义关联。 It would look something like this for your situation: 根据您的情况,它看起来像这样:

[Table(Name = "Customer")]
public class Customer
{
    [Column(IsPrimaryKey = true, Name = "ID")]
    public int ID { get; set; }

    [Column(Name = "Name")]
    public string Name { get; set; }

    [Association(Name = "Customer_Purchases", ThisKey = "ID", OtherKey = "CustomerID")]
    public EntitySet<Purchase> PurchaseList { get; set; }

    public List<Purchase> Purchases
    {
        get
        {
            return new List<Purchase>(PurchaseList.AsEnumerable());
        }
    }

}

[Table(Name = "Purchase")]
public class Purchase
{
    [Column(IsPrimaryKey = true, Name = "ID")]
    public int ID { get; set; }

    [Column(Name = "CustomerID")]
    public int CustomerID { get; set; }

    [Column(Name = "Description")]
    public string Description { get; set; }

    [Column(Name = "Price")]
    public decimal Price { get; set; }

}

You can then enumerate your customers like so: 然后,您可以像这样枚举客户:

var customers = customerTable.ToList();

foreach (Customer customer in customers)
{
    foreach (Purchase purchase in customer.Purchases)
    {
        Console.WriteLine("My data here...");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM