[英]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.