enum DeleteBehavior in FluentAPI has 3 values
While in Migration it is mapped to enum ReferentialAction which has 5 values
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?
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
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.
All right.
Could you give some info about second question, regarding [ForeignKey] and Index by default ?
One more thing, I don't see OnUpdatemethod in _FluentAPI_, and there is onUpdatein Migration.
Is it not implemented yet or?
And should I create new issue or it is already on task list?
Re OnUpdate: It doesn't make a lot of sense in the Fluent API because you can't update keys using EF.
@AndriySvyryd may know how to remove the index.
@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);
@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");
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.
@AndriySvyryd any comment regarding .RemoveIndex that is not working ?
@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.
@AndriySvyryd That worked,
Thx
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.
Most helpful comment
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.