Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to use the attach method to relate two many to many tables (Group and Student). However, this is not working by making use of the following code:

C#
using (var db = new ManyToManyContainer())
{
   //get existing learner
   var existingStudent = (from s in db.Students 
         where s.RegNo== "X1"
         select s).FirstOrDefault<Student>();

   //Get existing group
   var existingGroup = (from g in db.Groups
         where g.GroupName == "G1"
         select g).FirstOrDefault<Group>();

   existingStudent.Groups.Attach(existingGroup);
   db.SaveChanges();
}



The Attach method is not found and gives the error:

System.Collections.Generic.ICollection<ManyToManyAttatch.Group>' does not contain a definition for 'Attach' and no extension method 'Attach' accepting a first argument of type 'System.Collections.Generic.ICollection<ManyToManyAttatch.Group>' could be found (are you missing a using directive or an assembly reference?)

Below is my SQL Script that VS is using to create the tables.

SQL
-- Creating table 'Students'
CREATE TABLE [dbo].[Students] (
    [StudentId] int IDENTITY(1,1) NOT NULL,
    [RegNo] nvarchar(max)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [GroupId] int IDENTITY(1,1) NOT NULL,
    [GroupName] nvarchar(max)  NOT NULL,
    [GroupSize] int  NOT NULL
);
GO

-- Creating table 'StudentGroup'
CREATE TABLE [dbo].[StudentGroup] (
    [Students_StudentId] int  NOT NULL,
    [Groups_GroupId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [StudentId] in table 'Students'
ALTER TABLE [dbo].[Students]
ADD CONSTRAINT [PK_Students]
    PRIMARY KEY CLUSTERED ([StudentId] ASC);
GO

-- Creating primary key on [GroupId] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([GroupId] ASC);
GO

-- Creating primary key on [Students_StudentId], [Groups_GroupId] in table 'StudentGroup'
ALTER TABLE [dbo].[StudentGroup]
ADD CONSTRAINT [PK_StudentGroup]
    PRIMARY KEY CLUSTERED ([Students_StudentId], [Groups_GroupId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Students_StudentId] in table 'StudentGroup'
ALTER TABLE [dbo].[StudentGroup]
ADD CONSTRAINT [FK_StudentGroup_Student]
    FOREIGN KEY ([Students_StudentId])
    REFERENCES [dbo].[Students]
        ([StudentId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_GroupId] in table 'StudentGroup'
ALTER TABLE [dbo].[StudentGroup]
ADD CONSTRAINT [FK_StudentGroup_Group]
    FOREIGN KEY ([Groups_GroupId])
    REFERENCES [dbo].[Groups]
        ([GroupId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentGroup_Group'
CREATE INDEX [IX_FK_StudentGroup_Group]
ON [dbo].[StudentGroup]
    ([Groups_GroupId]);
GO


I am totally stuck. Please assist.
Posted
Updated 29-Dec-13 12:02pm
v5
Comments
Aydin Homay 29-Dec-13 1:39am    
Hi please add your entity relation diagram we can not diagnosis your problem with this information,But overall you should have teacher primary key from Group table for making one to many relation ship and you should have middle entity from group and learners for making many to many relation ship of course pay attention at this middle entity you should add group primary key and learners primary key ;-)
Wamuti 29-Dec-13 2:20am    
okay. I am working on it. Slow internet to upload :-(

1 solution

Hi, I solved this problem by using "Add" instead of Attach. This does not make sense since Add is used for entities that are none existence. But, it works despite that both Group and Student already exist. Below is the code:



C#
using (var db = new ManyToManyContainer())
                {
                    //get existing learner
                    var existingStudent = (from s in db.Students
                                           where s.RegNo == "X1"
                                           select s).FirstOrDefault<Student>();
                    //Get existing group

                    var existingGroup = (from g in db.Groups
                                         where g.GroupName == "G1"
                                         select g).FirstOrDefault<Group>();

                    existingStudent.Groups.Add(existingGroup);

                    db.SaveChanges();

                    success = true;


Thanks all the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900