问题描述
我在这里拉出我的头发。我见过的解决方案,在这里关闭级联删除上,但我不能执行。我不知道我做错了什么,但我不断收到以下错误:
System.Data.Entity.ModelConfiguration.EntityTypeConfiguration'不包含'WillCascadeOnDelete',没有扩展方法'WillCascadeOnDelete接受型System.Data.Entity.ModelConfiguration.EntityTypeConfiguration'的第一个参数的定义可以发现(是否缺少using指令或程序集引用?)
我已经添加了必要的命名空间,但我不认为这是一个选项中的任意位置智能感知,我不成气候搜索。我在VS 2010 MVC 3
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用vf2.Models;
使用vf2.Models.LinkTables;
使用vf2.Models.Requests;
使用System.Data.Entity的;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用System.Data.Entity.ModelConfiguration.Configuration;
使用System.Data.Entity.ModelConfiguration;
使用vf2.Models.Reporting;
使用vf2.Models.POSObj;命名空间vf2.Models
{
公共类vfContext:的DbContext
{
公共DbSet<应用>应用{搞定;组; }
公共DbSet<产地>起源{搞定;组; }
公共DbSet< WineType> WineTypes {搞定;组; }
公共DbSet< VarType函数> VarTypes {搞定;组; }
公共DbSet<葡萄酒与GT;葡萄酒{搞定;组; }
公共DbSet<复古与GT;年份{搞定;组; } 公共DbSet<分发商GT;经销商{搞定;组; }
公共DbSet<进口商和GT;进口商{搞定;组; }
公共DbSet<生产者>生产者{搞定;组; }
公共DbSet<刊物及GT;出版物{搞定;组; }
公共DbSet<回顾与GT;评论{搞定;组; }
公共DbSet<&用户类型GT; UserTypes {搞定;组; }
公共DbSet<餐厅>餐馆{搞定;组; } 公共DbSet< WineListChangeRate> WineListChangeRates {搞定;组; }
公共DbSet< MenuChangeRate> MenuChangeRates {搞定;组; }
公共DbSet< WineListCount> WineListCounts {搞定;组; } 公共DbSet< UserObj> UserObjs {搞定;组; }
公共DbSet< ProducerUser> ProducerUsers {搞定;组; }
公共DbSet< DistributorUser> DistributorUsers {搞定;组; }
公共DbSet< RestaurantUser> RestaurantUsers {搞定;组; } 公共DbSet< ProducerEditRequest> ProducerEditRequests {搞定;组; }
公共DbSet< RequestStatus> RequestStatuses {搞定;组; }
公共DbSet< VOAVIRequest> VOAVIRequests {搞定;组; } 公共DbSet< POS> POSS {搞定;组; }
公共DbSet<购物车和GT;车{搞定;组; }
公共DbSet< FutureUser> FutureUsers {搞定;组; }
公共DbSet<&文件GT;文档{搞定;组; }
公共DbSet<!DOCTYPE>文档类型{搞定;组; } 公共DbSet< WineVisit> WineVisits {搞定;组; }
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{ modelBuilder.Entity<回顾及GT;()WillCascadeOnDelete(假)。
//错误在这里! base.OnModelCreating(模型构建器);
}
}
}
级联删除是一个关系的构造实体/表不。因此, WillCascadeOnDelete
是 CascadableNavigationPropertyConfiguration
的方法。使用案例:
modelBuilder.Entity<回顾及GT;()
.HasRequired(R => r.Wine)
.WithMany()
.WillCascadeOnDelete(假);
这意味着,如果酒是从目录中的数据库中删除,这是审查不应与酒一起删除。这就是这个特定的关系的性质,而不是评论
表。
在这种情况下尝试删除有评论将导致违反外键约束,当然异常的酒,但是那是你平时想如果禁用级联删除所需关系(不允许以删除有评论葡萄酒,只允许其用于哪些还没有任何葡萄酒...)。
I'm pulling out my hair here. I've seen the solutions to turning off cascade on delete here, but I can't implement it. I don't know what I'm doing wrong here, but I keep getting the below error:
'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration' does not contain a definition for 'WillCascadeOnDelete' and no extension method 'WillCascadeOnDelete' accepting a first argument of type 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration' could be found (are you missing a using directive or an assembly reference?)
I've added the necessary namespaces, but I don't see it as an option anywhere in the intellisense and I'm not getting anywhere searching. I'm in 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);
}
}
}
"Cascading delete" is a configuration of a relationship, not of an entity/table. Hence WillCascadeOnDelete
is a method of CascadableNavigationPropertyConfiguration
. Use case example:
modelBuilder.Entity<Review>()
.HasRequired(r => r.Wine)
.WithMany()
.WillCascadeOnDelete(false);
It means that if a wine is deleted from the catalog in the database, it's reviews should not be deleted together with the wine. That's a property of this specific relationship, not of the Reviews
table.
In this case trying to delete a wine which has reviews would result in a foreign key constraint violation and exception of course, but that is what you usually want when you disable cascading delete on a required relationship ("Don't allow to delete a wine which has reviews, only allow it for wines which haven't any...").
这篇关于设置表级WillCascadeOnDelete不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!