gpt4 book ai didi

c# - AP.NET webforms 使用 LINQ to SQL 数据源和跨多个表的外键数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 21:49:16 25 4
gpt4 key购买 nike

我正在尝试构建一个 asp.net webforms 应用程序作为 sql 数据库的前端。

see database table relations in this image

简单地获取表值不是问题。例如,像这样显示“Item”表中的值:

var items = from i in db.Items
select new
{
ID = i.Item_ID,
Name = i.Name,
Barcode = i.BarCode,
Description = i.Description,
ItemType = i.ItemType1.ItemTypeName,
LocationCount = i.Location_Item_Juncs.Count
};
GridView1.DataSource = items;
GridView1.DataBind();

looks like this in webforms webpage

问题是获取某个项目的供应商信息。

一个项目可以有多个“Supplier”、“Location”和“ReceivedDate”!

在 SQL 中,我可以像这样查询该信息:

select Supplier.Name, Supplier.Adress, Supplier.Email, Supplier.Phone, Supplier.Supplier_Zipcode
from item, Supp_Company, Supplier
where Item_ID = 8 and Item_ID = ItemSub_ID and SupplierJunc_ID = Supplier_ID

results look like this in linqpad

这些是商品 ID 为 8 的商品的供应商信息。

请注意,查询涉及 3 个表(Item、Supp_Company、Supplier),并且必须匹配 2 对值才能选择有效值。

我想在 LINQ 中复制该查询以在我的网络表单应用程序中使用。我相信这个问题的解决方案也适用于获取项目的位置和“接收日期”。

是否可以像在 SQL 中那样在 LINQ 中使用类似的“where”子句?语法是什么样的?

最佳答案

当然可以,但这完全取决于您构建映射的方式。你在这里有很多场景,你可以

  • 依赖链表映射实体
  • 映射实体依赖链接实体

链接表方法(注意 modelBuilder HasMany WithMany)

void Main()
{
using (var context = new YourContext())
{
var query = from item in context.Items
from supplier in item.Suppliers
where item.ItemId == 8
select new
{
Name = supplier.Name,
Adress = supplier.Address,
Email = supplier.Email,
Phone = supplier.Phone,
Zip = supplier.Zip,
};

//...
}
}

public class YourContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Supplier> Suppliers { get; set; }

public YourContext() : base("MyDb")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasMany(item => item.Suppliers)
.WithMany(supplier => supplier.Items)
.Map(m =>
{
m.MapLeftKey("ItemSub_ID");
m.MapRightKey("SupplierJunc_ID");
m.ToTable("Supp_Company");
});
}
}

public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
}

public class Supplier
{
public int SupplierId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Zip { get; set; }
public ICollection<Item> Items { get; set; }
}

链接实体方法(注意modelBuilder 将每个表映射到一个实体)

void Main()
{
using (var context = new YourContext())
{
var query = from item in context.Items
join link in context.SupplierItems
on item.ItemId equals link.ItemId
join supplier in context.Suppliers
on link.SupplierId equals supplier.SupplierId
where item.ItemId == 8
select new
{
Name = supplier.Name,
Adress = supplier.Address,
Email = supplier.Email,
Phone = supplier.Phone,
Zip = supplier.Zip,
};

//...
}
}

public class YourContext : DbContext
{
public DbSet<Item> Items { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<SupplierItem> SupplierItems { get; set; }

public YourContext() : base("MyDb")
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
// ...
;

modelBuilder.Entity<Supplier>()
// ...
;

modelBuilder.Entity<SupplierItem>()
// ...
;
}
}

public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
}

public class Supplier
{
public int SupplierId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Zip { get; set; }
}

public class SupplierItem
{
public int ItemId { get; set; }
public int SupplierId { get; set; }
}

关于c# - AP.NET webforms 使用 LINQ to SQL 数据源和跨多个表的外键数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47990754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com