2008-09-08 5 views
1

Ich habe eine Beziehung zwischen zwei Entitäten (e1 und e2) und e1 hat eine Sammlung von e2, aber ich habe eine ähnliche Beziehung zwischen (e2 und e3), aber e2 enthält keine Sammlung von e3, warum auch immer das würde passieren? Alles, was ich schreiben kann, um das leichter herauszufinden?Aus welchem ​​Grund wird LINQ to SQL keine Sammlung basierend auf einer Beziehung generieren?

Edit: Ich habe gerade festgestellt, dass die Beziehung zwischen e1 und e2 ist fest und zwischen e2 und e3 ist punktiert, was verursacht das? Ist es verwandt?

+0

haben Sie es bekommen arbeiten, oder haben Sie immer noch Probleme? – KyleLanser

Antwort

0

die FK_Contraints sind wie folgt aufgebaut:..

ALTER TABLE [DBO] [e2] WITH CHECK ADD CONSTRAINT [FK_e2_e1] FOREIGN KEY ([E1Id]) LITERATUR [DBO] [e1] ([Id ])

ALTER TABLE [dbo]. [e3] mit Check-Constraint ADD [FK_e3_e2 FOREIGN KEY] ([E2Id]) LITERATUR [dbo]. [e2] ([Id])

ist das, was Sie fragte nach?

2

Mit diesem Setup funktionierte alles.

1) LINQ to SQL-Abfrage, 2) DB-Tabellen, 3) LINQ to SQL-Datenmodell in VS.NET 2008

1 - LINQ to SQL-Abfrage

DataClasses1DataContext db = new DataClasses1DataContext(); 

var results = from threes in db.tableThrees 
    join twos in db.tableTwos on threes.fk_tableTwo equals twos.id 
    join ones in db.tableOnes on twos.fk_tableOne equals ones.id 
    select new { ones, twos, threes }; 

2 - Datenbank-Scripts

--Table One 
CREATE TABLE tableOne(
    [id] [int] IDENTITY(1,1) NOT NULL, 
    [value] [nvarchar](50) NULL, 
CONSTRAINT [PK_tableOne] PRIMARY KEY CLUSTERED 
    ([id] ASC) WITH ( 
    PAD_INDEX = OFF, 
    STATISTICS_NORECOMPUTE = OFF, 
    IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, 
    ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
    ) ON [PRIMARY]; 

--Table Two 
CREATE TABLE tableTwo(
    [id] [int] IDENTITY(1,1) NOT NULL, 
    [value] [nvarchar](50) NULL, 
    [fk_tableOne] [int] NOT NULL, 
CONSTRAINT [PK_tableTwo] PRIMARY KEY CLUSTERED 
    ([id] ASC) WITH (
    PAD_INDEX = OFF, 
    STATISTICS_NORECOMPUTE = OFF, 
    IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, 
    ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
    ) ON [PRIMARY]; 

ALTER TABLE tableTwo WITH CHECK  
    ADD CONSTRAINT [FK_tableTwo_tableOne] 
    FOREIGN KEY([fk_tableOne]) 
    REFERENCES tableOne ([id]); 

ALTER TABLE tableTwo CHECK CONSTRAINT [FK_tableTwo_tableOne]; 


--Table Three 
CREATE TABLE tableThree(
    [id] [int] IDENTITY(1,1) NOT NULL, 
    [value] [nvarchar](50) NULL, 
    [fk_tableTwo] [int] NOT NULL, 
CONSTRAINT [PK_tableThree] PRIMARY KEY CLUSTERED 
    ([id] ASC) WITH (
    PAD_INDEX = OFF, 
    STATISTICS_NORECOMPUTE = OFF, 
    IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, 
    ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
    ) ON [PRIMARY]; 

ALTER TABLE tableThree WITH CHECK 
    ADD CONSTRAINT [FK_tableThree_tableTwo] 
    FOREIGN KEY([fk_tableTwo]) 
    REFERENCES tableTwo ([id]); 

ALTER TABLE tableThree CHECK CONSTRAINT [FK_tableThree_tableTwo]; 

3 - LINQ to SQL-Datenmodell in Visual Studio

alt text http://i478.photobucket.com/albums/rr148/KyleLanser/ThreeLevelHierarchy.png

Verwandte Themen