如何从联结表中访问数据?

时间:2022-10-05 11:53:05

I have created a junction table between two of my tables to create a many to many relationship between them.I am able to save data to them, but can't access that data again. This is written in MVC ASP.NET by the way.

我在两个表之间创建了一个联结表,以在它们之间创建多对多的关系。我能够将数据保存到它们,但无法再次访问这些数据。这是用MVC ASP.NET编写的。

My first table:

我的第一张桌子:

CREATE TABLE [dbo].[UserInfo] (
[Id]        INT             IDENTITY (1, 1) NOT NULL,
[FirstName] NVARCHAR (50)   NULL,
[LastName]  NVARCHAR (50)   NULL,
[Email]     NVARCHAR (256)  NOT NULL,
[Image]     VARBINARY (MAX) NULL,
[Approved]  BIT             NOT NULL,
[Color]     NVARCHAR (10)   NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

My second table:

我的第二张桌子:

CREATE TABLE [dbo].[Events] (
[Id]        NVARCHAR (128) NOT NULL,
[Name]      NVARCHAR (100) NOT NULL,
[StartDate] DATETIME       NULL,
[EndDate]   DATETIME       NULL,
[Approved]  BIT            NOT NULL,
[room_id]   INT            NULL,
[color]     NVARCHAR (10)  NOT NULL,
[Owner]     INT            NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Events_Rooms] FOREIGN KEY ([room_id]) REFERENCES [dbo].[Rooms] ([_key])
);

My junction table:

我的联络表:

CREATE TABLE [dbo].[UserToEvent] (
[UserId]  INT            NOT NULL,
[EventId] NVARCHAR (128) NOT NULL,
CONSTRAINT [UserId_EventId_pk] PRIMARY KEY CLUSTERED ([UserId] ASC, [EventId] ASC),
CONSTRAINT [FK_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[UserInfo] ([Id]),
CONSTRAINT [FK_Event] FOREIGN KEY ([EventId]) REFERENCES [dbo].[Events] ([Id])
);

Code to add new relationship:

代码添加新关系:

Event e = await db.Events.FindAsync(id);
string email = User.Identity.Name;
UserInfo user = db.UserInfoes.Where(x => x.Email == email).First();
user.Events.Add(e);
e.UserInfoes.Add(user);
db.Entry(user).State = EntityState.Modified;
db.Entry(e).State = EntityState.Modified;
await db.SaveChangesAsync();

Code to access relationship:

访问关系的代码:

UserInfo user = await db.UserInfoes.FindAsync(id);
List<Events> events = user.Events;

I would expect events to be filled with all events associated with user, but it never has any events.

我希望事件可以填充与用户相关的所有事件,但它永远不会有任何事件。

1 个解决方案

#1


0  

I have found a work-around which is sufficient to my needs. I added another attribute to my junction table, which then allowed me to access it within my controllers (Since there weren't only two primary keys). From here I was able to gather the data from it just like any other table. Not sure why the above method does not work however. I have used that method before without a hitch. If anyone has an answer, I would love to hear it.

我找到了一个足以满足我需求的解决方法。我在我的联结表中添加了另一个属性,然后允许我在我的控制器中访问它(因为不仅有两个主键)。从这里我可以像其他任何表一样从中收集数据。不知道为什么上述方法不起作用。我之前没有遇到过这种方法。如果有人有答案,我很乐意听到。

#1


0  

I have found a work-around which is sufficient to my needs. I added another attribute to my junction table, which then allowed me to access it within my controllers (Since there weren't only two primary keys). From here I was able to gather the data from it just like any other table. Not sure why the above method does not work however. I have used that method before without a hitch. If anyone has an answer, I would love to hear it.

我找到了一个足以满足我需求的解决方法。我在我的联结表中添加了另一个属性,然后允许我在我的控制器中访问它(因为不仅有两个主键)。从这里我可以像其他任何表一样从中收集数据。不知道为什么上述方法不起作用。我之前没有遇到过这种方法。如果有人有答案,我很乐意听到。