RockStar
RockStar

Reputation: 87

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:

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.

Upvotes: 0

Views: 2039

Answers (2)

S4V1N
S4V1N

Reputation: 279

Try this :

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

Upvotes: 0

Win
Win

Reputation: 62260

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();

Upvotes: 1

Related Questions