Association with Different Column Names in Entity Framework - entity-framework

I have a table Person with a Primary Key of PersonId. I have another table CheckDetails with a column named DirectorId. There is a Foreign Key between Person.PersonId and CheckDetails.DirectorId. It is a 1-to-Many relationship
I created 3 entities: Person, Director, and CheckDetail.
Person maps back to the Person table and has PersonId as its key.
Director inherits from Person and has an ICollection of CheckDetail.
CheckDetail has Director property named Director.
I used the following when mapping Director:
HasRequired( d => d.CheckDetails ).WithMany( ).HasForeignKey( d => d.PersonId );
but I get the following error: The foreign key component 'PersonId' is not a declared property on type 'Director'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
How do I create this association?

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.

Self-referencing Many to Many relationship in EF Database first

Is there a way to map multiple foreign keys to the same navigation property of an entity? I have a table with a self-referencing many to many relationship defined by a relationship table.
For example, I have a Person table and I want to define the different related people. I created another table, PersonRelationship, that defines those relationships with 2 foreign keys to the Person table. It doesn't matter if the person is referenced in the first or the second foreign key just that the person is in one. I would like my Person entity to have a list of all the relationships but instead it has two lists of relationships: one in which the person is in the first foreign key and another where it is the second foreign key.
How can I map these two foreign keys so that they map to the same list in the Person entity? (I am using a database first approach)

How to tell EntityFramework 5.0 that there's a one-to-one association between two entities?

I could go into the EDMX in design view and update the cardinality (1-, 0-, etc.) of associations, but I want to know if it is alright to do that in this scenario.
I have these two tables, let's say:
User
-----
Id
UserProfile
-------------
Id
UserId
Stuff
A single user may have just one profile. Therefore, I have set a UNIQUE constraint no the UserId column in the UserProfile table. Also, UserProfile.Id is defined as a foreign key that references User.Id.
However, in the Entity Framework model, I get the following:
class User
{
ICollection<UserProfile> UserProfiles { get; set; }
}
At first, I had the UserProfile table without any primary key of its own like this:
UserProfile
--------------
UserId
Stuff
But EntityFramework made that read-only as it needed to have a primary key on every table that you want to be made writable. So, I put an Id column in the UserProfile table as well.
UPDATE
I tried setting the multiplicit/cardinality of the association between the User table and the UserProfile table in my EDMX in the designer. However, I get this error suggesting I should turn it back to what it was, i.e. a one-to-many relationship.
Running transformation: Multiplicity is not valid in Role 'UserBasicProfile'
in relationship 'FK_UserBasicProfile_User'. Because the Dependent Role
properties are not the key properties, the upper bound of the multiplicity
of the Dependent Role must be *.
I've found a way. Well, not purely a 1-1 relationship but a 1-0..1 relationship was what I was after, which, for all practical purposes is a one-to-one relationship.
If you ever fall into this trap like I described in my question, here's what you do:
1) Remove the Id field from the dependent table, in this case the UserProfile table. Therefore, do not give it a separate primary key.
2) Instead, mark the foreign key in the dependent table as its own primary key. In this case, mark the UserId field itself as the primary key of the UserProfile table.
In addition to these two, I assume, as I outlined in the question that you've got a primary and foreign key relationship between the authority (the User table in this case) and the dependent table (the UserProfile table in this example).
So, you new table structure should look like this:
User
------
Id
UserProfile
-------------
UserId (foreign key, and also the primary key of this UserProfile table)
Stuff
Entity Framework will then recognize this 1-0..1 relationship.

JPA 2 one to many - how does JPA infer column information?

I have a JPA2 (Hibernate) application which uses a MySQL database with only two tables. One table is called "companies" and the other table is called "employees". Between the two tables there is a one-to-many ralationship (1 company has many employees). The foreign-key column in table "employees" is called "company_id".
In my JPA2 Application I use the following annotations:
In the entity class "Company" I have the following annotation
#OneToMany(cascade = CascadeType.ALL)
private Collection<Employee> employees;
and in class Employee
#ManyToOne
private Company company;
How does JPA know what column it should use to determine all employees of a company. The annotations do not hold this information, but the application works.
Thank you
The ManyToOne side is missing the optional JoinColumn annotation, which in turn has the optional name attribute defaulting to:
The concatenation of the following: the name of the referencing relationship property or field of the referencing entity or embeddable class; "(underscore)"; the name of the referenced primary key column. If there is no such referencing relationship property or field in the entity, or if the join is for an element collection, the join column name is formed as the concatenation of the following: the name of the entity; "(underscore)"; the name of the referenced primary key column.
On the other side of the relationship, the OneToMany side, it's missing the mappedBy attribute (it should be equal to the name of the field that owns the relationship, in your case "company"). Javadoc says that this attribute is required unless the relationship is unidirectional, so there are chances that the JPA implementation you are using is assuming the relationship is unidirectional.