感谢您的关注。 在开发会计程序期间,我遇到了一个问题,需要一些帮助工程师。
我需要实体:产品和单元
[Table("Products")]
public class Product
{
#region Properties
private int _Id;
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Name;
[Required]
[MaxLength(50)]
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private string _Code;
[Required]
[MaxLength(20)]
public string Code
{
get { return _Code; }
set { _Code = value; }
}
private int _MainUnitId;
[Required]
public int MainUnitId
{
get { return _MainUnitId; }
set { _MainUnitId = value; }
}
private int _SubsidiaryUnitId;
[Required]
public int SubsidiaryUnitId
{
get { return _SubsidiaryUnitId; }
set { _SubsidiaryUnitId = value; }
}
private int _SnachUnitId;
[Required]
public int SnachUnitId
{
get { return _SnachUnitId; }
set { _SnachUnitId = value; }
}
private decimal _SubidiaryCount;
[Required]
public decimal SubidiaryCount
{
get { return _SubidiaryCount; }
set { _SubidiaryCount = value; }
}
private decimal _SnachCount;
[Required]
public decimal SnachCount
{
get { return _SnachCount; }
set { _SnachCount = value; }
}
#endregion Proerties
#region Navigators
private Unit _MainUnit;
[ForeignKey("MainUnitId")]
public virtual Unit MainUnit
{
get { return _MainUnit; }
set { _MainUnit = value; }
}
private Unit _SubsidiaryUnit;
[ForeignKey("SubsidiaryUnitId")]
public virtual Unit SubsidiaryUnit
{
get { return _SubsidiaryUnit; }
set { _SubsidiaryUnit = value; }
}
private Unit _SnachUnit;
[ForeignKey("SnachUnitId")]
public virtual Unit SnachUnit
{
get { return _SnachUnit; }
set { _SnachUnit = value; }
}
#endregion Navigators
}
这是我的单位实体:
[Table("Units")]
public class Unit
{
private int _Id;
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Title;
[Required]
[MaxLength(50)]
public string Title
{
get { return _Title; }
set { _Title = value; }
}
private ICollection<Product> _MainUnitedProducts;
[InverseProperty("MainUnit")]
public virtual ICollection<Product> MainUnitedProducts
{
get { return _MainUnitedProducts; }
set { _MainUnitedProducts = value; }
}
private ICollection<Product> _SubsidiaryUnitedProducts;
[InverseProperty("SubsidiaryUnit")]
public virtual ICollection<Product> SubsidiaryUnitedProducts
{
get { return _SubsidiaryUnitedProducts; }
set { _SubsidiaryUnitedProducts = value; }
}
private ICollection<Product> _SnachUnitedProducts;
[InverseProperty("SnachUnit")]
public virtual ICollection<Product> SnachUnitedProducts
{
get { return _SnachUnitedProducts; }
set { _SnachUnitedProducts = value; }
}
}
如你所见,我在这些内心之间有一对多的关系:
Product.MainUnitId *-----1 Unit.Id
Product.SubsidiaryUnitId *-----1 Unit.Id
Product.SnachUnitId *-----1 Unit.Id
但是当我使用Context.Database.Create()时发生意外错误
在表'Products'上引入FOREIGN KEY约束'FK_Products_Units_SubsidiaryUnitId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
有没有人帮助我?! 我是否正确使用InverProperty映射和ForeignKey映射?
谢谢你们
Foroughi
答案 0 :(得分:2)
异常来自SQL Server。您必须关闭三个关系的级联删除(它由映射约定启用,因为您的关系是必需的而不是可选的)。遗憾的是,您不能使用数据注释,而只能使用Fluent API:
modelBuilder.Entity<Product>()
.HasRequired(p => p.MainUnit)
.WithMany(u => u.MainUnitedProducts)
.HasForeignKey(p => p.MainUnitId)
.WillCascadeOnDelete(false);
其他两种关系也一样。您可以删除[ForeignKey]
和[InverseProperty]
注释,因为Fluent API中的此映射已经定义了反向和外键属性。