QUERY: Error making the Update-Database due to problems with foreign keys

0

Good day, afternoon or evening, I'm doing a small test project by taking an inventory. I'm working on C # ASP.Net Core 2.0 I have the following classes: Inventory customers Suppliers Sales Products

I did the Update-Database and everything went normal, then I thought that just as I did inventory exits, I also had to make entries to it, so I created my Entradas model, which basically does the same as sales but instead of subtracting , add to the inventory, but at the time of making my Update-Database I get the following error: Introducing FOREIGN KEY constraint 'FK_Entries_Provider_ProviderId' on table 'Entries' 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 or index. See previous errors.

These are my classes: PRODUCT CLASS:

public class Producto
{
    [Required]
    public int ProductoId { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string ReferenciaFabrica { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string ReferenciaLocal { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public double PrecioCompra { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public double PrecioVenta { get; set; }

    public int MarcaId { get; set; }
    public int ProveedorId { get; set; }
    public int TipoFiltroId { get; set; }

    public Marca Marca { get; set; }
    public Proveedor Proveedor { get; set; }
    public TipoFiltro TipoFiltro { get; set; }

    public ICollection<Ventas> Ventas { get; set; }
    public ICollection<Entradas> Entradas { get; set; }
}

PROVIDER CLASS

public class Proveedor
{
    public int ProveedorId { get; set; }

    [Required]
    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "{0} solo puede contener caracteres")]
    public string Nombre { get; set; }

    [Required]
    [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "{0} solo puede contener caracteres")]
    public string Apellidos { get; set; }

    [Required]
    [StringLength(30, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string Telefono { get; set; }

    [Required]
    [StringLength(70, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string Direccion { get; set; }

    public ICollection<Producto> Producto { get; set; }
    public ICollection<Entradas> Entradas { get; set; }
}

CLASS INPUTS:

public class Entradas
{
    [Key]
    [Required]
    public int EntradaID { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaCompra { get; set; }

    [Required]
    public int Cantidad { get; set; }


    public int ProductoId { get; set; }
    public Producto Producto { get; set; }

    public int ProveedorId { get; set; }
    public Proveedor Proveedor { get; set; }
}

INVENTORY CLASS:

public class Inventario
{
    [Required]
    public int InventarioID { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaInventario { get; set; }

    [Required]
    public int Stock { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "{0} debe tener una longitud de {1} caracteres.")]
    public string Ubicacion { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FechaSalida { get; set; }

    public int ProductoId { get; set; }
    public Producto Producto { get; set; }

}

I do not know what to do to update my database and start inserting data, I hope your help, thank you very much

    
asked by Tyg0th 07.03.2018 в 16:37
source

1 answer

0

Your problem is common in ASP.Net this type of errors or warnings occur so that you know that there are relationships between tables that can bring the elimination in Casacada of data, I mean what that error tells you is that if you delete a Provider All Entries that have that Provider will be eliminated. To solve this you can go to the last migration created when you changed your model when creating the Inputs class and look for the block of code where the Entry Table is declared. I give you an example of what you should see

In the Migrations folder of your project, select the last migration and find where the Entry Table has been declared.

migrationBuilder.CreateTable(
            name: "Entradas",
            columns: table => new
            {
                Id = table.Column<int>(type: "INTEGER", nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                ....
                ....
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Entradas", x => x.Id);
                table.ForeignKey(
                    name: "FK_Entradas_Proveedor_ProveedorId",
                    column: x => x.ProveedorId,
                    principalTable: "Proveedores",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.NoAction);
            });

in table.ForeignKey() you must have declared: name:"FK_Entradas_Proveedor_ProveedorId".... onDelete: ReferentialAction.Cascade change it by ReferentialAction.NoAction and try your Update-Database. I hope it helps you

    
answered by 12.03.2018 в 14:12