我使用此步骤在我的 MVC 项目中生成 CodeFirst 类:
1) 在成员(member)支持下运行我的应用程序并调用使用成员(member)创建成员(member)默认表(Membership、User、UserInRoles...)的 MVC Action
2) 将新的 .edmx 文件添加到我的项目中并在向导中选择“从数据库生成”
3) 在 Visual Studio 中编辑 .edmx 文件中的数据库(添加新表)
4) 在 .edmx 中使用 “从模型生成数据库” 创建新数据库
5) 将 Entity Framework Power Tools Beta 3 与“Reverse Engineer Code First”
结合使用6) 删除现有数据库并调用使用我的上下文的 MVC 操作
对于这种情况是否有更简单的方法?
我收到这个错误:
在表 'UsersInRoles' 上引入 FOREIGN KEY 约束 'FK_dbo.UsersInRoles_dbo.Users_Users_UserId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。查看以前的错误。
请指导我有关错误以及任何简单快速的方法。
谢谢。
最佳答案
看起来您可能需要禁用“级联删除”。
从此链接查看此帖子 EF Fluent API
Enabling Cascade Delete
You can configure cascade delete on a relationship by using the WillCascadeOnDelete method. If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null.
You can remove these cascade delete conventions by using:
modelBuilder.Conventions.Remove() modelBuilder.Conventions.Remove()
The following code configures the relationship to be required and then disables cascade delete.
modelBuilder.Entity<Course>()
.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID)
.WillCascadeOnDelete(false);
所有这些都应该在您的 EF 上下文文件中。
注意:您可能会想到禁用它的弊端!
祝你好运!
关于c# - 结合 EF Power Tools 和 .edmx 生成 CodeFirst Class 和 DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16182961/