locked
how we can change "On Delete : Referential Action" in asp.net core RRS feed

  • Question

  • User640994516 posted

    Hi All

    I am working on asp.net core with "code first approach". On my first migration I have created some tables and there constraints in database.

    Now I want to change the referential action from Restrict to No Action using code first approach?

    onDelete: ReferentialAction.Restrict
     migrationBuilder.CreateTable(
                    name: "SupplierCurrencies",
                    columns: table => new
                    {
                        SupplierId = table.Column<int>(nullable: false),
                        CurrencyId = table.Column<int>(nullable: false),
                        DefaultCurrency = table.Column<int>(nullable: false)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_SupplierCurrencies", x => new { x.SupplierId, x.CurrencyId });
                        table.ForeignKey(
                            name: "FK_SupplierCurrencies_Currency_CurrencyId",
                            column: x => x.CurrencyId,
                            principalTable: "Currency",
                            principalColumn: "Id",
                            onDelete: ReferentialAction.Restrict);
                        table.ForeignKey(
                            name: "FK_SupplierCurrencies_Supplier_SupplierId",
                            column: x => x.SupplierId,
                            principalTable: "Supplier",
                            principalColumn: "Id",
                            onDelete: ReferentialAction.Restrict);
                    });

    If we change the model and run add-migration it will Add/Remove column names but how we can change Referential Action?

    Friday, September 18, 2020 6:20 PM

Answers

  • User1312693872 posted

    Hi,ali84pk

    Cascade delete is commonly used in database terminology to describe a characteristic that allows the deletion of a row to automatically trigger the deletion of related rows. 

    No Action is equivalent to Restrict in most databases, the difference is that Restrict results in the constraint being checked at the start of processing instead of at the end.

    If you want to change it to No Action, you should better not to change the code in migration class, you should change the OnModelCreating() and the add-migration again.

    modelBuilder.Entity<***>()
               .HasOne(i => i.***)
               .WithMany(c => c.***)
               .IsRequired()
               .OnDelete(DeleteBehavior.NoAction);

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2020 7:20 AM

All replies

  • User585649674 posted

    It would be easy to do in database by using SQL. The code first migration is only for easy start up of the projects. Many features such as creation of index, partition, colation has to be done using SQL.

    Sunday, September 20, 2020 2:57 PM
  • User1312693872 posted

    Hi,ali84pk

    Cascade delete is commonly used in database terminology to describe a characteristic that allows the deletion of a row to automatically trigger the deletion of related rows. 

    No Action is equivalent to Restrict in most databases, the difference is that Restrict results in the constraint being checked at the start of processing instead of at the end.

    If you want to change it to No Action, you should better not to change the code in migration class, you should change the OnModelCreating() and the add-migration again.

    modelBuilder.Entity<***>()
               .HasOne(i => i.***)
               .WithMany(c => c.***)
               .IsRequired()
               .OnDelete(DeleteBehavior.NoAction);

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2020 7:20 AM
  • User640994516 posted

    It would be easy to do in database by using SQL. The code first migration is only for easy start up of the projects. Many features such as creation of index, partition, colation has to be done using SQL.

    so in this case when we will deploy the project on live server and run all the pending migrations, we will need to correct these relationships manually directly from database? Or there is any other way of creating additional migration to correct the relationship type?

    Thursday, September 24, 2020 12:04 AM