AP.NET使用LINQ to SQL数据源进行webforms,并在多个表中使用外键进行数据绑定

问题描述 投票:1回答:1

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

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

c# mysql sql asp.net linq
1个回答
0
投票

确定你可以,这一切都取决于你构建映射的方式。你在这里有很多场景,你可以

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

链接表方法(注意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; }
}
© www.soinside.com 2019 - 2024. All rights reserved.