将 FOREIGN KEY 约束 'FK_dbo.Employees_dbo.Admins_AdminID' 引入表 'Employees' 可能会导致循环或多重级联路径。请指定 ON DELETE
来源:互联网 发布:加工中心圆怎么编程 编辑:程序博客网 时间:2024/06/06 04:54
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 管理员表
/// </summary>
public class Admins
{
[Key]
public int AdminID { get; set; }//管理员编号
[MaxLength(16)]
[Column("AdminUser", TypeName = "varchar")]
public string AdminUser { get; set; }//管理员账号
[MaxLength(16)]
[Column("AdminPWD", TypeName = "varchar")]
public string AdminPWD { get; set; }//密码
[MaxLength(20)]
[Column("AdminName", TypeName = "nvarchar")]
public string AdminName { get; set; }//姓名
[Required]
public virtual ICollection<Employees> Employees { get; set; }
[Required]
public virtual ICollection<Permanents> Permanents { get; set; }//导航属性
[Required]
public virtual ICollection<Dimissions> Dimissions { get; set; }//导航属性
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 转正信息
/// </summary>
public class Permanents
{
[Key]
public int PerID { get; set; }//转正信息编号
[Required]
public int EmpID { get; set; }//员工编号
[Required]
public int AdminID { get; set; }//创建人
public PerType PerType { get; set; }//转正类型
[Column("PerDate", TypeName = "date")]
public DateTime PerDate { get; set; }//转正日期
[MaxLength(500)]
[Column("PerReMark", TypeName = "nvarchar")]
public string PerReMark { get; set; }//转正备注
[Required]
[Column("PerCreateDate", TypeName = "datetime")]
public DateTime PerCreateDate { get; set; }//创建时间
[Required]
public virtual Employees Employees { get; set; }
[Required]
public virtual Admins Admins { get; set; }
}
public enum PerType
{
正常转正 = 1,
提前转正,
推迟转正
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 职位表
/// </summary>
public class Dutys
{
[Key]
public int DutyID { get; set; }//职位编号
[MaxLength(20)]
[Column("DutyName",TypeName="nvarchar")]
public string DutyName { get; set; }//职位名称
[Required]
public virtual ICollection<Employees> Employees { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 离职信息
/// </summary>
public class Dimissions
{
[Key]
public int DimID { get; set; }//离职信息编号
[Required]
public int EmpID { get; set; }//员工编号
[Column("DimDate", TypeName = "date")]
public DateTime DimDate { get; set; }//离职时间
public DimType DimType { get; set; }//离职类型
public DimCause DimCause { get; set; }//离职原因
[Column("DimCreateDate", TypeName = "datetime")]
public DateTime DimCreateDate { get; set; }//创建时间
[Required]
public int AdminID { get; set; }//创建人编号
[Required]
public virtual Employees Employees { get; set; }
[Required]
public virtual Admins Admins { get; set; }
}
public enum DimType
{
离职 = 1,
劝退,
辞退,
自离
}
public enum DimCause
{
薪酬福利 = 1,
身体原因,
人际关系,
家庭原因,
发展空间,
上级主管,
能力欠缺,
出差原因,
其他
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 部门表
/// </summary>
public class Departs
{
[Key]
public int DepID { get; set; }//部门编号
[MaxLength(20)]
[Column("DepName", TypeName = "nvarchar")]
public string DepName { get; set; }//部门名称
[Required]
public virtual ICollection<Employees> Employees { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TL.DAL
{
/// <summary>
/// 合同表
/// </summary>
public class Contracts
{
[Key]
public int ConID { get; set; }//合同编号
public int EmpID { get; set; }//员工编号
[Column("ConDate", TypeName = "date")]
public DateTime ConDate { get; set; }//签订时间
[Column("ConBeginDate", TypeName = "date")]
public DateTime ConBeginDate { get; set; }//合同开始时间
[Column("ConEndDate", TypeName = "date")]
public DateTime ConEndDate { get; set; }//合同结束时间
[MaxLength(200)]
[Column("ConRemark", TypeName = "nvarchar")]
public string ConRemark { get; set; }//备注
[Required]
public virtual Employees Employees { get; set; }
}
}
namespace TL.DAL
{using System;
using System.Data.Entity;
using System.Linq;
using TL.DAL;
public class MyContext : DbContext
{
public MyContext() : base("dbcon") { }
public virtual DbSet<Dutys> Dutys { get; set; }
public virtual DbSet<Departs> Departs { get; set; }
public virtual DbSet<Admins> Admins { get; set; }
public virtual DbSet<Contracts> Contracts { get; set; }
public virtual DbSet<Dimissions> Dimissions { get; set; }
public virtual DbSet<Permanents> Permanents { get; set; }
public virtual DbSet<Employees> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employees>().HasRequired(u => u.Admins)
.WithMany(d => d.Employees)
.HasForeignKey(c => c.AdminID)
.WillCascadeOnDelete(false);
}
}
}
上面代码是能成功建库,并新增数据的最终版。
1、去掉OnModelCreating重载方法
报错:
将 FOREIGN KEY 约束 'FK_dbo.Employees_dbo.Admins_AdminID' 引入表 'Employees' 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
无法创建约束。请参阅前面的错误消息。
或
SqlException: Introducing FOREIGN KEY constraint 'FK_dbo.Employees_dbo.Admins_AdminID' on table 'Employees' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.Could not create constraint. See previous errors.
分析原因:EF创建的FK默认是带级联
因为SqlServer并不是真正运行去校验是否循环或多重级联,而是通过一个级联路径(可能与Name有关系)
如果主键列不叫ID,也没有问题,但这样EF在建库时,会自动创建一列外键列(如Admins_AdminID在代码中看不到)
- 将 FOREIGN KEY 约束 'FK_dbo.Employees_dbo.Admins_AdminID' 引入表 'Employees' 可能会导致循环或多重级联路径。请指定 ON DELETE
- FOREIGN KEY 约束的ON UPDATE CASCADE 和 ON DELETE CASCADE
- mysql foreign key ... references ... on delete cascade
- MySQL表中有外键约束时级联删除报错annot delete or update a parent row: a foreign key constraint fails
- DB2外键删除规则的特殊约束(DB2 FOREIGN KEY ON DELETE RULES special constraints)
- FOREIGN KEY 约束
- FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- foreign key 约束(重点)
- SQL FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- SQL FOREIGN KEY 约束
- oracle约束+disable与enable约束+为表添加约束+修改表约束的状态+primary key+not null+unique+foreign key+外键删除数据,可以使用的级联操作
- Oracle之外键(Foreign Key)用法详解(二)- 级联删除(DELETE CASCADE)
- 级联删除报Cannot delete or update a parent row: a foreign key constraint fails解决方法
- Array(6) -- Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix I, II
- C语言宏定义,内置宏,__FILE__,__LINE__,## 用法
- 统计学习方法之感知机对偶形式Java实现代码
- 电信光纤——光猫——路由器,如何映射,外网访问
- UVa - 10048 - Audiophobia ( Floyd 变形 )
- 将 FOREIGN KEY 约束 'FK_dbo.Employees_dbo.Admins_AdminID' 引入表 'Employees' 可能会导致循环或多重级联路径。请指定 ON DELETE
- 能量函数在神经网络中的含义
- 堆(heap)和栈(stack)的区别
- 设计模式之代理模式
- UVa 202 - Repeating Decimals
- 本地maven环境配置
- android Fragment动态加载的使用详解
- 2016年12月19日学习总结----C语言程序优化
- 数据结构之单链表的简单操作