我在这里拉出我的头发。我在这里看到了关闭删除级联的解决方案,但我无法实现它。我不知道我在这里做错了什么,但我一直得到以下错误:
'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration'不包含'WillCascadeOnDelete'的定义,并且没有扩展方法'WillCascadeOnDelete'接受类型为'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration'的第一个参数' (您是否缺少using指令或程序集引用?)
我已经添加了必要的命名空间,但我没有在intellisense的任何地方看到它作为一个选项,我没有得到任何搜索。我在VS 2010 MVC 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vf2.Models;
using vf2.Models.LinkTables;
using vf2.Models.Requests;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration;
using vf2.Models.Reporting;
using vf2.Models.POSObj;
namespace vf2.Models
{
public class vfContext : DbContext
{
public DbSet<App> Apps { get; set; }
public DbSet<Origin> Origins { get; set; }
public DbSet<WineType> WineTypes { get; set; }
public DbSet<VarType> VarTypes { get; set; }
public DbSet<Wine> Wines { get; set; }
public DbSet<Vintage> Vintages { get; set; }
public DbSet<Distributor> Distributors { get; set; }
public DbSet<Importer> Importers { get; set; }
public DbSet<Producer> Producers { get; set; }
public DbSet<Publication> Publications { get; set; }
public DbSet<Review> Reviews { get; set; }
public DbSet<UserType> UserTypes { get; set; }
public DbSet<Restaurant> Restaurants { get; set; }
public DbSet<WineListChangeRate> WineListChangeRates { get; set; }
public DbSet<MenuChangeRate> MenuChangeRates { get; set; }
public DbSet<WineListCount> WineListCounts { get; set; }
public DbSet<UserObj> UserObjs { get; set; }
public DbSet<ProducerUser> ProducerUsers { get; set; }
public DbSet<DistributorUser> DistributorUsers { get; set; }
public DbSet<RestaurantUser> RestaurantUsers { get; set; }
public DbSet<ProducerEditRequest> ProducerEditRequests { get; set; }
public DbSet<RequestStatus> RequestStatuses { get; set; }
public DbSet<VOAVIRequest> VOAVIRequests { get; set; }
public DbSet<POS> POSs { get; set; }
public DbSet<Cart> Carts { get; set; }
public DbSet<FutureUser> FutureUsers { get; set; }
public DbSet<Doc> Docs { get; set; }
public DbSet<DocType> DocTypes { get; set; }
public DbSet<WineVisit> WineVisits { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Review>().WillCascadeOnDelete(false);
//error here!
base.OnModelCreating(modelBuilder);
}
}
}
答案 0 :(得分:9)
&#34;级联删除&#34;是关系的配置,而不是实体/表的配置。因此WillCascadeOnDelete
是CascadableNavigationPropertyConfiguration
的方法。用例示例:
modelBuilder.Entity<Review>()
.HasRequired(r => r.Wine)
.WithMany()
.WillCascadeOnDelete(false);
这意味着如果从数据库中的目录中删除葡萄酒,则不应将其与葡萄酒一起删除。这是这种特定关系的属性,而不是Reviews
表的属性。
在这种情况下,尝试删除具有评论的葡萄酒会导致外键约束违规和当然例外,但这是您在所需关系上禁用级联删除时通常需要的(&#34; Don&# 39;允许删除有评论的葡萄酒,只允许用于没有葡萄酒的葡萄酒......&#34;)。