问题描述
大家好,
我正在尝试使用attach方法将两个多表联系起来(组和学生)。但是,这不能通过使用以下代码来实现:
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:
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();
}
找不到Attach方法并给出错误:
System.Collections.Generic.ICollection< ManyToManyAttatch.Group>'不包含'Attach'的定义,也没有扩展方法'Attach'接受a可以找到类型'System.Collections.Generic.ICollection< ManyToManyAttatch.Group>'的第一个参数(您是否缺少using指令或汇编引用?)
以下是VS用于创建表的SQL脚本。
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.
-- 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.
推荐答案
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.
这篇关于关联已有数据的多对多表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!