c# - EntityFramework 抛出错误 : The table does not have a primary key defined

标签 c# sql-server asp.net-mvc entity-framework asp.net-web-api

我在 Entity Framework 中使用 DatabaseFirst 方法,出现错误:

The table does not have a primary key defined.

现在,当我为 Controller 搭建脚手架时,它会给我

One or More Entities have Validation Error

肯定是EF错误影响到这里了。

早些时候,当它给我主键错误时,我在表 StudentAddress & StudentCourse 中添加了列,并使其成为 Primary KEY摆脱上述错误。但是添加的列不会反射(reflect)在我的图表 (*.edmx) 中,尽管在 DB 中它会反射(reflect)回来。

数据库脚本是:

create table [Standard](

    StandardID int constraint pk_standard primary key,
    StandardName varchar(255),
    [Description] varchar(255)
);


create table Teacher(
    TeacherID int constraint pk_teacher primary key,
    TeacherName varchar(255),
    StandardID int constraint fk_teacher foreign key references [Standard](StandardID) ,
    TeacherType varchar(255)
);
create table Course(
    CourseID int constraint pk_course primary key,
    CourseName varchar(255),
    Location varchar(255),
    TeacherID int constraint fk_course foreign key references Teacher(TeacherID) ,
);
create table Student(
    StudentID int constraint pk_student primary key,
    StudentName varchar(255),
    StandardID int constraint fk_student foreign key references [Standard](StandardID) ,
);
create table StudentAddress(
    [Id] int constraint pk_SA primary key,
    StudentID int constraint fk_studentaddr foreign key references Student(StudentID) not null,
    Addr1 varchar(255),
    Addr2 varchar(255),
    City varchar (50),
    [State] varchar (50)
);
create table StudentCourse(
    [Index Number] int constraint pk_SC primary key,
    StudentID int constraint fk1_studentcourse foreign key references Student(StudentID) not null,
    CourseID int constraint fk2_studentcourse foreign key references Course(CourseID) not null,
);

PS:使用 WebApi2 和 EF(用于数据)创建 WebApp。

最佳答案

我打赌你没有在原始模型中为 StudentAddressStudentCourse 定义主键。现在,通过向其中添加列,您可以显着改变模型的特征。

我认为这就是您想要的:

create table StudentAddress(
    StudentID int constraint fk_studentaddr foreign key references Student(StudentID) not null,
    Addr1 varchar(255),
    Addr2 varchar(255),
    City varchar (50),
    [State] varchar (50),
    constraint pk_SA primary key (StudentID) -- primary key
);
create table StudentCourse(
    StudentID int constraint fk1_studentcourse foreign key references Student(StudentID) not null,
    CourseID int constraint fk2_studentcourse foreign key references Course(CourseID) not null,
    constraint pk_SC primary key (StudentID, CourseID) -- composite primary key
);

这使得 Student-StudentAddress 成为 1:1 关联,而 Student-StudentCourse 成为 n:m 关联.因此,Student-StudentCourse可以隐藏在类模型中。

所以你应该只添加主键约束而不是

以下是添加列的结果:

通过添加另一列作为 StudentAddress 的主键,关联 Student-StudentAddress 变成了 1:n 关联(因为 StudentAddress.StudentID 现在可以复制)。

有了额外的 Index Number 列,Student-StudentCourse 仍然是一个 n:m 关联,但它不能再隐藏在类模型,因为这只有在联结表仅包含两个外键且都包含一个主键的情况下才有可能。

我会根据上面的模型创建一个适合您的新 edmx。使用修改后的主键刷新模型可能效果不佳。

修改后的 edmx 应该是这样的:

enter image description here

关于c# - EntityFramework 抛出错误 : The table does not have a primary key defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072144/

相关文章:

java - 如何将 sqljdbc_auth.dll 添加到我的 Maven 项目

c# - MVC 4 RedirectToAction 看不到自定义 header

c# - 服务与程序交互

c# - 强类型 List.GroupBy()

java - 如何将相同的值写入2个数据库?

asp.net-mvc - ASP.NET MVC 中的 session 变量

asp.net-mvc - MVC 中的多个 OutputCache

c# - TPL数据流,对核心设计感到困惑

c# - 平衡客场和主场比赛算法

.net - 有没有办法通过在 SQL Server 中使用 SQL 参数来指定临时表中的列长度?