c# - 结合 EF Power Tools 和 .edmx 生成 CodeFirst Class 和 DB

标签 c# asp.net-mvc entity-framework code-first ef-power-tools

我使用此步骤在我的 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/

相关文章:

c# - 多线程无法正常执行

c# - 如何在 LINQ 中比较两个匿名变量列表

jquery - 使用 select2 将空值添加到 DropDownListFor

c# - jQuery - 更改下拉列表索引也会更改值

c# - 使用 EF6 在运行时连接到 SQL Server - 部分类构造函数的问题

c# - 正则表达式 [\S 而不是 [[\S?

c# - Visual Studio 无法发现测试 (xunit)

asp.net-mvc - 何时以及为什么要考虑使用 asp.net MVC?

c# - EF 创建数据库并在其名称中添加 guid

c# - Entity Framework 核心 : use auto-generated foreign key as part of composite primary key