2013-04-24 57 views
3

我用这一步在我的MVC项目产生CodeFirst类:结合EF电动工具和的.edmx产生CodeFirst类和DB

1)会员支持运行我的应用程序,并调用使用会员创建MVC行动会员默认表(会员,用户,UserInRoles,...)

2)添加新的.edmx文件到我的项目,并选择在向导 “从数据库生成”

3)编辑DB中的.edmx Visual Studio中的文件(添加新表格)

4)创建新的数据库中的.edmx

5)使用实体框架电动工具测试版3 “反向工程代码优先”

6)删除现有数据库“生成模型数据库”并调用使用我的上下文的MVC操作

对于这种情况,是否有更简单的方法?

我得到这个错误:

表“UsersInRoles”引进国外KEY约束“FK_dbo.UsersInRoles_dbo.Users_Users_UserId”可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束。查看以前的错误。

请指导我关于错误和任何简单快速的方法。

谢谢。

+0

没有人有任何想法吗? – b24 2013-04-25 14:34:55

回答

0

看起来您可能需要禁用“级联删除”。

入住这个帖子从这个链接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上下文文件。

注意:您可能会考虑禁用它的缺点!

祝你好运!

相关问题