简体   繁体   English

通过EF4.3中的代码优先方法模型在实体之间创建多个(一对多)关系

[英]Create more than one (one to many) relations between to entity by code first approach model in EF4.3

thanks for attention. 谢谢你的关注。 During developing an accounting program, I faced with a flowing problem and need some help engineers. 在开发会计程序期间,我遇到了一个问题,需要一些帮助工程师。

I have to entities : Product and unit 我必须要实体: 产品单位

 [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

}

and this is my Unit Entity : 这是我的单位实体:

[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; }
    }

}

As you see i have have tree one-to-many relation between these entitis : 如你所见,我在这些entitis之间有一对多的关系:

Product.MainUnitId        *-----1     Unit.Id
Product.SubsidiaryUnitId  *-----1     Unit.Id
Product.SnachUnitId       *-----1     Unit.Id

but when i use the Context.Database.Create() an unexpected error occurred 但是当我使用Context.Database.Create()时发生了意外错误

Introducing FOREIGN KEY constraint 'FK_Products_Units_SubsidiaryUnitId' on table 'Products' may cause cycles or multiple cascade paths. 在表'Products'上引入FOREIGN KEY约束'FK_Products_Units_SubsidiaryUnitId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Is there anyone out there to help me?! 有没有人在那里帮助我?! Did I use InverProperty mapping and ForeignKey mapping right? 我是否使用InverProperty映射和ForeignKey映射?

thanks guys 多谢你们

Foroughi Foroughi

The exception comes from SQL Server. 例外来自SQL Server。 You will have to turn off cascading delete for the three relationships (it is on by mapping conventions because your relationships are required and not optional). 您必须关闭三个关系的级联删除(它由映射约定启用,因为您的关系是必需的而不是可选的)。 Unfortunately you can't do this with data annotations but only with Fluent API: 遗憾的是,您无法使用数据注释,但只能使用Fluent API:

modelBuilder.Entity<Product>()
    .HasRequired(p => p.MainUnit)
    .WithMany(u => u.MainUnitedProducts)
    .HasForeignKey(p => p.MainUnitId)
    .WillCascadeOnDelete(false);

And the same for the other two relationships. 其他两种关系也一样。 You can remove then the [ForeignKey] and [InverseProperty] annotations because this mapping in Fluent API already defines the inverse and foreign key properties. 您可以删除[ForeignKey][InverseProperty]注释,因为Fluent API中的此映射已经定义了反向键和外键属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF 4.3编码第一个一对一关系 - EF 4.3 code first one-to-one relations 首先使用实体​​代码的3个表之间的一对多关系 - one to many relations between 3 tables with entity code first EF Core Code First,相同object的2个多对一关系 - EF Core Code First, 2 Many to one relations of same object EF 代码一对多 - EF Code First One To Many 如何在EF4.3代码优先的情况下映射到复杂类型? - How do I map to and from a complex type in EF4.3 code-first? EF代码首先将许多模型关联到一个模型 - EF code first relation many models to one model 首先如何在代码中定义两个实体之间的多对多和一对多关系? - how to define many-to-many and one-to-many relations between two entities in code first? 实体框架代码优先:如何在两个表之间创建一对多和一对一的关系? - Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 无需代码优先模型创建的一对多关系实体框架 - One To Many relationship entity framework without Code First Model Creation EF 5代码优先:两个实体之间的一对一和一对多关联 - EF 5 Code First: one-to-one and one-to-many associations between two entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM