Skip to content

FK DeleteBehavior missing Enum values #7530

Closed as not planned
Closed as not planned

Description

@borisdj

enum DeleteBehavior in FluentAPI has 3 values

  • Restrict
  • SetNull
  • Cascade

While in Migration it is mapped to enum ReferentialAction which has 5 values

  • NoAction
  • Restrict
  • Cascade
  • SetNull
  • SetDefault

Shouldn't they have same values, or even to have only one Enum for both usage?

Since this is about FKs I will use it to ask one more question here .
In EF core default behavior for [ForeignKey] is that it automatically generates Index on column.
Is it possible and how, in fluentAPI, to configure on Column something like HasNoIndex, so that Index in not created in migration.
The idea is to have only FK relationship but without Index?

Further technical details

EF Core version: 1.1.0
Database Provider: (Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Windows 10 Home
IDE: (Visual Studio Community 2015 Update 3)
SQL Server 2016 Developer Edition

Activity

  1. bricelam commented on Feb 2, 2017

    @bricelam
    Contributor

    The Fluent API is about how EF handles relationships between loaded entities when a principal end is deleted. The Migrations API is about how the database handles foreign key columns when the referenced row is deleted.

    NoAction typically means Restrict in most databases (except SQLite where it means literally take no action and leave the data in an inconsistent state). SetDefault is typically used with non-nullable foreign keys or to assign orphaned records to a specific parent. The Migrations API is designed to be a DSL over DDL and tries to enable all DDL scenarios. Another example is that it lets you set a foreign key on update action even though EF won't let you update entity keys. This allows you to create any kind of database even though you may have to "drop down" to raw SQL at times to interact with it the way you want.

    In summary, Migrations has more for the sake of completeness, and the Fluent API has less because the other ones don't make as much sense in an object model.

  2. borisdj commented on Feb 2, 2017

    @borisdj
    Author

    All right.

    Could you give some info about second question, regarding [ForeignKey] and Index by default ?

  3. borisdj commented on Feb 2, 2017

    @borisdj
    Author

    One more thing, I don't see OnUpdate method in FluentAPI, and there is onUpdate in Migration.
    Is it not implemented yet or?
    And should I create new issue or it is already on task list?

  4. bricelam commented on Feb 2, 2017

    @bricelam
    Contributor

    Re OnUpdate: It doesn't make a lot of sense in the Fluent API because you can't update keys using EF.

  5. bricelam commented on Feb 2, 2017

    @bricelam
    Contributor

    @AndriySvyryd may know how to remove the index.

  6. AndriySvyryd commented on Feb 2, 2017

    @AndriySvyryd
    Member

    @borisdj The easiest way is to do the following at the end of OnModelCreating:

    var index = modelBuilder.Entity<MyEntity>().HasIndex(e => e.Name).Metadata;
    index.DeclaringEntityType.RemoveIndex(index.Properties);
    
  7. borisdj commented on Feb 2, 2017

    @borisdj
    Author

    @AndriySvyryd Thanks for code snippet but it's now working.
    In Entity class Movie there is:
    [ForeignKey("Genre")]
    public int GenreId { get; set; }
    public virtual Genre Genre { get; set; }

    And even when I add following at the end of OnModelCreating method:
    var index = modelBuilder.Entity<Movie>().HasIndex(e => e.GenreId).Metadata;
    index.DeclaringEntityType.RemoveIndex(index.Properties);

    Index still gets created after add-migration:

       migrationBuilder.CreateIndex(
           name: "IX_Movies_GenreId",
           table: "Movies",
           column: "GenreId");
    
  8. isukces commented on Feb 3, 2017

    @isukces

    So based on arguments above ReferentialAction.NoAction should be removed at all because is useless (SQLite) or redundant (other dbs). OnUpdate is not supported becuase there is no scenario for it. The same situation is with ReferentialAction.NoAction.

    What about real live scenatio that uses SetDefault?. Usually when creating database with tables and foreign keys we don't know ID of record that can substitute owner role when relation is broken. So setting default value of field in table A can (should) be set after inserting some default record to table B. For sure we can force to insert record into table B with some special ID value (like 0 or Guid.Empty) but I don't think that it is situation that has background in real business cases.

    In my opinion using SetDefault directly or not is a signal about wrong approach.

  9. borisdj commented on Feb 8, 2017

    @borisdj
    Author

    @AndriySvyryd any comment regarding .RemoveIndex that is not working ?

  10. AndriySvyryd commented on Feb 8, 2017

    @AndriySvyryd
    Member

    @borisdj Try this less supported snippet:

    var index = (Index)modelBuilder.Entity<MyEntity>().HasIndex(e => e.Name).Metadata;
    index.DeclaringEntityType.RemoveIndex(index.Properties, runConventions: false);
    

    Alternatively you can remove the CreateIndex call from the migration.

  11. borisdj commented on Feb 8, 2017

    @borisdj
    Author

    @AndriySvyryd That worked,
    Thx

  12. rowanmiller commented on Feb 13, 2017

    @rowanmiller
    Contributor

    Created a new issue for the ability to remove indexes.

    First reply from @bricelam covers why we don't have the additional cascade delete options in the top level API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions