简体   繁体   English

如何使用asp.net实体框架在SQL外键中获取数据?

[英]How to get data in an sql foreign key using asp.net entity framework?

I'm now trying to incorporate relational database in my project. 我现在正在尝试将关系数据库合并到我的项目中。 Before the implementation of rdbms in my project, normally i would normally be able to access my desired value through this code: 在我的项目中实施rdbms之前,通常我通常可以通过以下代码访问所需的值:

public static string guitar { get; set; }
public static List<stringInstrumentItem> GetGuitarItems()
{
    List<stringInstrumentItem> list2 = new List<stringInstrumentItem>();

        MusicStoreDBEntities obj2 = new MusicStoreDBEntities();
        list2 = (from g in obj2.stringInstrumentItems where g.brand == guitar select g).ToList();


    return list2;

}

But now that i already set a foreign key in my sql script, the g.brand is not working because it does not contain a text value anymore, it is now an int called brandId. 但是,既然我已经在sql脚本中设置了外键,则g.brand无法正常工作,因为它不再包含文本值,它现在是一个称为brandId的int。 To better understand my problem, here is the sql script for stringInstrumentItem table. 为了更好地理解我的问题,这是stringInstrumentItem表的sql脚本。 This is where I set the foreign key brandId referencing the brand table and its primary key. 这是我设置引用品牌表及其主键的外键brandId的地方。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[stringInstrumentItem](
[itemId] [int] NOT NULL,
[type] [varchar](50) NOT NULL,
[brandId] [int] FOREIGN KEY REFERENCES brand(brandId),
[model] [varchar](50) NOT NULL,
[price] [float] NOT NULL,
[itemimage1] [varchar](255) NULL,
[itemimage2] [varchar](255) NULL,
[description] [text] NOT NULL,
[necktype] [varchar](100) NOT NULL,
[body] [varchar](100) NOT NULL,
[fretboard] [varchar](100) NOT NULL,
[fret] [varchar](50) NOT NULL,
[bridge] [varchar](100) NOT NULL,
[neckpickup] [varchar](100) NOT NULL,
[bridgepickup] [varchar](100) NOT NULL,
[hardwarecolor] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED 
(
[itemId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = 
OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Now here is the brand table. 现在是品牌表。 This is the table that stringInstrumentItem is referring to. 这是stringInstrumentItem所引用的表。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[brand](
[brandId] [int] NOT NULL,
[type] [varchar](50) NOT NULL,
[name] [varchar](50) NOT NULL,
[image] [varchar](255) NULL,
PRIMARY KEY CLUSTERED 
(
[brandId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = 
OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF 
GO

My goal is to be able to access the value of a field called name in brand table through stringInstrumentItem table using asp.net and entity framework. 我的目标是能够使用asp.net和实体框架通过stringInstrumentItem表访问品牌表中名为name的字段的值。 I haven't really found answers for this. 我还没有真正找到答案。 I hope you guys can help me on this one. 我希望你们能在这一方面帮助我。

If you have proper relationship between those two inside MusicStoreDBEntities, you should be able to access brand name like g.brand.name . 如果这两个MusicStoreDBEntities内部之间具有适当的关系,则应该能够访问g.brand.name类的品牌名称。

list2 = (from g in obj2.stringInstrumentItems 
        where g.brand.name == guitar select g).ToList();
                     ^^^^^^

Otherwise, you will have to manually join them like this - 否则,您将需要像这样手动加入他们-

list2 = (from g in obj2.stringInstrumentItems
        join b in obj2.brand
        on g.brandId equals b.brandId
        where b.name == guitar
        select g).ToList();

Try this : 尝试这个 :

    list2 = MusicStoreDBEntities.stringInstrumentItem. 
Where(x=> x.brand.name == "guitar").Select(x=> x).ToList();

暂无
暂无

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

相关问题 我如何在Asp.Net MVC中使用Entity Framework和Web Api获取外键表数据 - How do i get foreign key table data using Entity Framework and Web Api in Asp.Net MVC 如何使用asp.net获取SQL Server外键中的数据? - How to get data in a SQL Server foreign key using asp.net? 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc 如何使用ASP.NET MVC中的Entity Framework将记录插入到具有外键的表中 - How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC 没有实体框架外键关系的ASP.NET MVC - Asp.net mvc without entity framework foreign key relationships 使用实体框架的代码优先方法在ASP.NET MVC应用程序中缺少外键关系 - Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework ASP.NET C#实体框架-如何正确更新外键? - 第2部分 - ASP.NET C# Entity Framework - How to update Foreign Key properly? - Part 2 ASP.NET MVC如何访问Entity Framework生成的外键? - ASP.NET MVC How to access Entity Framework generated foreign key? ASP.NET C#实体框架-如何正确更新外键? - ASP.NET C# Entity Framework - How to update Foreign Key properly? 如何在 Entity Framework Core 和 ASP.NET Core MVC 中使用外键 - How work with foreign key in Entity Framework Core & ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM