【问题标题】:AP.NET webforms using LINQ to SQL datasource and databind with foreign keys across multiple tablesAP.NET webforms 使用 LINQ to SQL 数据源和跨多个表的外键数据绑定
【发布时间】:2018-06-08 00:20:53
【问题描述】:

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

see database table relations in this image

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

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

问题是获取某件商品的供应商信息之类的信息。

一个项目可以有多个“供应商”、“位置”和“接收日期”!

在 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 中复制该查询以在我的 Web 表单应用程序中使用。 我相信这个问题的解决方案也适用于获取物品的位置和“收到日期”。

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

【问题讨论】:

标签: c# mysql sql asp.net linq


【解决方案1】:

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

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

链接表方法(注意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; }
}

【讨论】:

  • 请注意,目前 EF Core 不支持通过链接表的多对多关系,因此需要映射导航所需的所有表
猜你喜欢
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 1970-01-01
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多