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

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)

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'

How to avoid Null Insert on Required TPH Columns for types that don't implement the required column

I've get a set of Class that utilize TPH inheritance.
So all of the types are in the "Documents" table.
Some document types have unique fields. If a sub document type has a non nullable field, the other types are getting an error when creating the entity, because EF tries to insert NULL into that column in the DB - because the subtype doesn't know about that column.
I have tried using defaultValue in a migration to default it to zero, but get the same result.
Is it possible to have a non nullable field in an inherited type, where the base type does not have that field at all?
Or will that always result in the "cannot insert the value Null in column x" error?
In this case the column is an enum.
Should I make it nullable and force it to be required some other way?

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

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

How to handle varchar columns with Entity Framework?

I have a table with a varchar column and I am using Entity Framework to use this column in the WHERE clause.
Entity Framework generates the query with N'' hence the index on the column cannot be used. Is there a way to force Entity Framework to generate varchar query instead of nvarchar one?
It actually depends on how you built your EF model, if you're using its Designer you can specify the required data type for each column (in your case simply set varchar and you're done).
If you're using a code-first approach you have to decorate the property that represents that column with the proper attribute (string objects in .NET are always Unicode so it'll map nvarchar by default), just do this (with data annotations, if you're using StringAttribute then se its IsUnicode property to false):
[Column(TypeName = "varchar")]
public string YourColumnName
{
get;
set;
}
You can use the EntityFunctions.AsNonUnicode(string) method, so then the EF will not pass the string value as nvarchar. I had the same issue with EF 5 and EDMX, where the Oracle database was ignoring a varchar2 column index, and that's worked for me.
var q = (from TableClass t in TableName
where t.varchar2Column == EntityFunctions.AsNonUnicode(someText));
MSDN reference: https://msdn.microsoft.com/pt-br/library/system.data.objects.entityfunctions(v=vs.110).aspx

Problem with Entity Framework Model Designer and SQL_Variant datatype

I have a table that has a column with SQL_Variant type and some other columns with types like int, bigint,...
When I add this table to edmx file it adds all columns but the SQL_Variant typed column.
Is there a bug or I have to do something to add that column?
The entity framework doesn't support sql_variant. If you have to use that type in your code, you've to use another o/r mapper.
There is a solution for read-only access.