简体   繁体   中英

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 :

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

Introducing FOREIGN KEY constraint 'FK_Products_Units_SubsidiaryUnitId' on table 'Products' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Is there anyone out there to help me?! Did I use InverProperty mapping and ForeignKey mapping right?

thanks guys

Foroughi

The exception comes from 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM