Entity Framework Table Per Hierarchy(TPH) Guid foreign key discriminator error - entity-framework

I have a Table per Hierarchy but when i made a Guid foreign key as the discriminator column, the migration failed with error:-Condition can not be specified on values of member ''. Value conditions are not supported for type 'SqlServer.uniqueidentifier'.
This is my configuration of one of my derived classes
Map(m => m.Requires("TypeId").HasValue("58287E26-7D9C-4CA3-84FA-163D7DD911B6"));
note:- i also tried with Map(m => m.Requires("TypeId").HasValue(new Guid("58287E26-7D9C-4CA3-84FA-163D7DD911B6"))); but also, the same error happened

Your discriminator can't be a foreign key: https://social.msdn.microsoft.com/Forums/en-US/24380ee6-4753-46a2-a3ed-b1cb2e2d161c/edm-tph-with-foreign-key-as-discriminator-column?forum=adodotnetentityframework

Related

Override Entity Framework Core convention for primary key in a join table

Consider two models, Book and Author, with a many-to-many relationship between them. By default, EF Core creates a join table with a composition key as its primary key.
I need my own primary key for the join table. I wonder if there is a way to "override" this convention, and define a property that will be a primary key for the join table.
I have tried to solve it with Fluent API, telling EF Core explicitly which table many-to-many relation shall be resolved into, giving it a property Id, and setting it as a primary key
builder
.Entity<Book>()
.HasMany(p => p.Author)
.WithMany(p => p.Book)
.UsingEntity(j => j.ToTable("Book_Author"));
builder.Entity("Book_Author")
.Property(typeof(int), "Id");
builder.Entity("Book_Author").HasKey("Id");
But I got an exception
Cannot use table 'Book_Author' for entity type 'Book_Author (Dictionary<string, object>)' since it is being used for entity type 'BookAuthor (Dictionary<string, object>)' and potentially other entity types, but there is no linking relationship. Add a foreign key to 'Book_Author(Dictionary<string, object>)' on the primary key properties and pointing to the primary key on another entity type mapped to 'Book_Author'

Creation of table foreign key constraints in database with JPA

In JPA project, when I generate tables for entities I can't see any table foreign key constraints created on database side.
I concluded that JPA cannot create table foreign key constraints in database and that referential integrity is enforced in JPA (on application side) and not by database.
Can someone confirm if this is so?
According to the JPA 2.2 specification, the persistence manager should create foreign key constraints. For example in the case of a one-to-one mapping:
Assuming that:
Entity A references a single instance of Entity B.
Entity B references a single instance of Entity A.
Entity A is specified as the owner of the relationship.
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.

Entity Framework Primary/Foreign constraint name

Is there any way to rename a primary/foreign key constraint name through either data annotations or fluent API?
I'm not talking about the property/column name. The constraint name. I did some search and didn't find any solutions yet.
Please help!
with .HasConstraintName
like you can see in the underlying example
entity.HasOne(d => d.Director)
.WithMany(p => p.Movie)
.HasForeignKey(d => d.DirectorId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Movie_Person");
It's not possible to customize the foreign key constraint name with Data annotations or Fluent API.
There is some options that you can follow:
Implement a custom sql generator class derived from SqlServerMigrationSqlGenerator from System.Data.Entity.SqlServer
When the tables get created via migrations, the migration code that gets automatically generated, so you can set a custom constraint name(s)
When the tables already exist you can drop the constraint and add a new renamed one in a migration

EF TPH - can I use a boolean column as a discriminator?

I am using Entity Framework.
Is it possible to use a boolean column as a discriminator column in Table Per Hierarchy (TPH) scenario?
I do not control the database.
Yes, it is possible. Assume you have discriminator column named EntityType:
modelBuilder.Entity<ParentEntity>()
.Map<DerivedA>(m => m.Requires("EntityType").HasValue(true))
.Map<DerivedB>(m => m.Requires("EntityType").HasValue(false));
That requires discriminator column to be of type (bit, null)

How do you signify a primary key in EF Code First?

I am new to EF Code First. How do you go about representing that a variable in an object should be the primary key when it is peristed to a database table?
By default and by convention, if you have a column called ID or (EntityName)ID (e.g. CustomerID for an entity of type Customer), then that will be your primary key.
Otherwise, you need to use the [Key] attribute on another column.