我正在使用Entity Framework 4.1
开发ASP.NET MVC3应用程序我的问题是尝试在两个实体之间创建关系以允许我使用下拉菜单中的值。当我使用以下代码时,我收到内部异常错误
InnerException {"Introducing FOREIGN KEY constraint 'TrafficCop_PLCModule' on table 'TrafficCops' may cause cycles or multiple cascade paths.
我有两个实体,PLCModules和TrafficCop
在TrafficCop中,我正在尝试从PLCModule Name列创建值的下拉菜单。
这是我的PLCModule实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace EDWv2.Models
{
public class PLCModule
{
public int PLCModuleID { get; set; }
public string Name { get; set; }
public int PLCModuleControlProcessorMfgID { get; set; }
public int PLCModuleDescID { get; set; }
public int PLCModuleRemarksID { get; set; }
public virtual PLCModuleControlProcessorMfg PLCModuleControlProcessorMfg { get; set; }
public virtual PLCModuleDesc PLCModuleDesc { get; set; }
public virtual PLCModuleRemarks PLCModuleRemarks { get; set; }
public virtual ICollection<TrafficCop> TrafficCop { get; set; }
}
}
这是我的TrafficCop实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace EDWv2.Models
{
public class TrafficCop
{
public int TrafficCopID { get; set; }
public string DropRack { get; set; }
public string Slot { get; set; }
public string AddressStart { get; set; }
public string AddressEnd { get; set; }
public string Drawing { get; set; }
public int ControlSystemDesignationID { get; set; }
public int PLCModuleDescID { get; set; }
public int PLCModuleID { get; set; }
public virtual PLCModule PLCModule { get; set; }
public virtual PLCModuleDesc PLCModuleDesc { get; set; }
public virtual ControlSystemDesignation ControlSystemDesignation { get; set; }
}
}
这是我的DbContext文件
public class EDWContext : DbContext
{
public DbSet<EDWSystem> EDWSystem { get; set; }
public DbSet<ControlSystemDescription> ControlSystemDescription { get; set; }
public DbSet<ControlSystemDesignation> ControlSystemDesignation { get; set; }
public DbSet<ControlProcessorMfg> ControlProcessorMfg { get; set; }
public DbSet<ControlProcessorModel> ControlProcessorModel { get; set; }
public DbSet<ControlProcessorProgram> ControlProcessorProgram { get; set; }
public DbSet<PLCModule> PLCModule { get; set; }
public DbSet<PLCModuleControlProcessorMfg> PLCModuleControlProcessorMfg { get; set; }
public DbSet<PLCModuleDesc> PLCModuleDesc { get; set; }
public DbSet<PLCModuleRemarks> PLCModuleRemarks { get; set; }
public DbSet<PlantLayout> PlantLayout { get; set; }
public DbSet<PlantLayoutBuildingDesig> PlantLayoutBuildingDesig { get; set; }
public DbSet<PlantLayoutBuildingFloor> PlantLayoutBuildingFloor { get; set; }
public DbSet<PlantLayoutBuildingUse> PlantLayoutBuildingUse { get; set; }
public DbSet<PlantLayoutFireProtection> PlantLayoutFireProtection { get; set; }
public DbSet<PlantLayoutFireSystem> PlantLayoutFireSystem { get; set; }
public DbSet<PlantLayoutFireMonitorCH> PlantLayoutFireMonitorCH { get; set; }
public DbSet<TrafficCop> TrafficCop { get; set; }
public DbSet<Abbreviation> Abbreviation { get; set; }
public DbSet<PreferredFieldDevice> PreferredFieldDevice { get; set; }
public DbSet<SupplyVoltage> SupplyVoltage { get; set; }
public DbSet<ControlVoltage> ControlVoltage { get; set; }
public DbSet<SignalVoltage> SignalVoltage { get; set; }
public DbSet<Communication1> Communication1 { get; set; }
public DbSet<Communication2> Communication2 { get; set; }
public DbSet<Communication3> Communication3 { get; set; }
}
谢谢,
杰森
编辑:
通过使用Fluent API,我可以使用类似的东西,但我不认为这是正确的方法。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
这是我的最终解决方案
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<TrafficCop>()
.HasOptional(p => p.PLCModules)
.WithMany()
.HasForeignKey(p => p.PLCModuleID)
.WillCascadeOnDelete(false);
}