Override Entity Framework Core convention for primary key in a join table - entity-framework-core

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'

Related

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.

EF Core one-to-many without Foreign Key (FK)

Are there ways to make one-to-many (abstract) relation without using SQL foreign keys?
I know it could be made by joining 2 non-related tables. But is it possible to use EF Core default navigation tools?
I think you want that because you can't add a foreign-key to your entity
So, you can solve that so:
You have a entity
Group
and you want a
List<User>
but your User can't be assigned to one group.
So you create a
GroupUser
entity which has a foreign key for the
List<GroupUser>
and a navigation property to the User
This is called a many-to-many relationship

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

MapKey vs HasForeignKey Difference - Fluent Api

What is actually the difference between:
this.HasRequired(a => a.Something)
.WithMany()
.Map(a => a.MapKey("SomethingId"));
and
this.HasRequired(a => a.Something)
.WithMany()
.HasForeignKey(a => a.SomethingId);
Both mappings will create exactly the same database schema with a non-nullable foreign key SomethingId and a referential constraint between the two related tables.
The first mapping with MapKey is used when you don't want to have the foreign key as a property in your model class. The type of association in this case is called Independent Association. You would apply the second mapping with HasForeignKey when the foreign key is a property in the model. This type is called Foreign Key Association.
In many scenarios it is easier to work with Foreign Key Associations, but many people consider it as less clean to have a relational artifact (a foreign key) in the object world and prefer Independent Associations therefore.
Here are some references about the two types of associations and their Pros and Cons:
http://www.ladislavmrnka.com/2011/05/foreign-key-vs-independent-associations-in-ef-4/
What are Independent Associations and Foreign Key Associations?
Code First: Independent associations vs. Foreign key associations?

Entity Framework Compound Key (Many-To-Many Relationship with a Payload)

I've got database tables like this:
A person may be a member of many teams. A team may have many members. Each person may have a position (think job title) within the team.
I've tried to set this up with ADO.NET Entity Framework and get errors:
Error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.PersonId is mapped to
<MembershipPerson.Membership.PersonId,
MembershipPerson.Person.Id>
and
error 3021: Problem in mapping
fragments starting at line ... Each of
the following columns in table
Membership is mapped to multiple
conceptual side properties:
Membership.TeamID is mapped to
<MembershipTeam.Membership.TeamId,
MembershipTeam.Team.Id>
The primary key of my Membership entity is a compound key of two foreign keys. I think that's the problem.
What must I do differently?
This happens if you use independent association on the property which is both part of primary key and foreign key. EFv4 introduced Foreign key associations (the difference is described here) and once you expose foreign key in the entity you must define foreign key association. After defining referential constraints delete mapping of independent association in Mapping details window.