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

為{幸葍}努か 提交于 2019-12-24 19:16:57

问题


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:

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. To better understand my problem, here is the sql script for stringInstrumentItem table. This is where I set the foreign key brandId referencing the brand table and its primary key.

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.

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. I haven't really found answers for this. I hope you guys can help me on this one.


回答1:


If you have proper relationship between those two inside MusicStoreDBEntities, you should be able to access brand name like 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();



回答2:


Try this :

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


来源:https://stackoverflow.com/questions/44825494/how-to-get-data-in-an-sql-foreign-key-using-asp-net-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!