1

我在两个实体(e1 和 e2)之间有一个关系,并且 e1 有一个 e2 的集合,但是我在(e2 和 e3)之间建立了类似的关系,但是 e2 不包含 e3 的集合,这有什么理由发生?我可以发布任何内容以使这更容易弄清楚吗?

编辑:我刚刚注意到 e1 和 e2 之间的关系是稳固的,而 e2 和 e3 之间的关系是点状的,这是什么原因造成的?有关系吗?

4

2 回答 2

2

使用此设置,一切正常。

1) LINQ to SQL 查询,2) DB 表,3) VS.NET 2008 中的 LINQ to SQL 数据模型

1 - LINQ to SQL 查询

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 - 数据库脚本

--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 - Visual Studio 中的 LINQ to SQL 数据模型

替代文字 http://i478.photobucket.com/albums/rr148/KyleLanser/ThreeLevelHierarchy.png

于 2008-10-06T19:11:49.453 回答
0

FK_Contraints 设置如下:

ALTER TABLE [dbo].[e2] WITH CHECK ADD CONSTRAINT [FK_e2_e1] FOREIGN KEY([E1Id]) REFERENCES [dbo].[e1] ([Id])

ALTER TABLE [dbo].[e3] WITH CHECK ADD CONSTRAINT [FK_e3_e2] FOREIGN KEY([E2Id]) REFERENCES [dbo].[e2] ([Id])

这是你要的吗?

于 2008-09-08T23:34:15.710 回答