2013-04-24 49 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上下文文件。

注意:您可能會考慮禁用它的缺點!

祝你好運!