Map parent child relationship in EntityFramework on top of existing database - entity-framework

i need to map some parent children relationship on top of a legacy database, the problem is how the tables are structured.
tableA
id
name
tableB
id
tableA_parentId
tableA_childId
As showed I have two foreign key from tableA to tableB
Is there a way to map this relationship with EF i'm using the code first approach and have a configuration class per table

Related

jpa mapping on three tables that are associated with foreign key

i have below 3 tables
Table A: Table B: Table C:
id TableA id(foreign key) id
type TableC id(foreign key) name
i have requirement where i have to fetch everything from tableA but when i fetch i have to fetch the name associated from Table C based on the mapping from TableB.
it's a oneToOne mapping. i tried adding Embeddable , joincolumn but nothing works.
i was referring this link - https://vladmihalcea.com/the-best-way-to-map-a-many-to-many-association-with-extra-columns-when-using-jpa-and-hibernate/ and implemented same but still no luck.
how would i achieve this through spring boot JPA?
Can somebody help me on this?

How can I join two tables by a foreign key table in Entity Framework?

I am trying to join two tables by a foreign key table in linq.
The two tables are
Items and Classes, and the foreign key table is ItemClasses (ItemId, ClassId).
My context does not have a DBSet for the foreign key table, and when I try to add it I get a model creation error.
When I saw the error, I noticed that this code referred to the foreign key table
modelBuilder.Entity<Classes>()
.HasMany(e => e.Items)
.WithMany(e => e.Classes)
.Map(m => m.ToTable("ItemClasses").MapLeftKey("ClassId").MapRightKey("ItemId"));
So it looks like I should be able to refer to the items through the class, but how do I join the tables?
I'd like to do something like this
query = from ig in myCtx.Items.AsNoTracking()
//join di in myCtx.ItemClasses.AsNoTracking() on ig.Id equals di.ClassId
join c in myCtx.Classes.AsNoTracking() on di.ClassId equals c.Id
join dd in myCtx.SalesAssociates.AsNoTracking() on dd.Id equals ig.SalesAssociateId
How do I do joins when the fk table is not in my context, but is referred to in one of the tables?
First, you have a configuration error. With HasMany / WithMany you are actually configuring the automatic "link" table, so
m.ToTable("Classes")
should be
m.ToTable("ItemClasses")
or how exactly you want that table to be named in the database.
Second, when working with EF, it's always preferred to use navigation properties rather than joins. In case of many-to-many relationship with auto link table, using navigation properties is mandatory. EF will produce the necessary joins for you.
So instead of join your would use something like this:
query = from ig in myCtx.Items.AsNoTracking()
from c in ig.Classes
...

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)

Entity Framework Discriminated Associations

Is there any way of getting discriminated associations to work in Entity Framework 4? That is, where we have the following tables
TableA
RelatedEntityTypeId
RelatedEntityTypeKey
TableB (1)
Id
TableC (2)
Id
TableD (3)
Id
and I want to have three associations on the entity for TableA:
TableB
TableC
TableD
which are defined by the RelatedEntityTypeId and RelatedEntityTypeKey fields...when RelatedEntityTypeId = 1, then the association is to EntityB, when RelatedEntityTypeId = 2, then the association is to EntityC, etc.
Thanks.
I don't know your purpose of doing this. I have used following approach to solve similer problem.
You can define a base type for all three tables (A,B,C). And when you want retrieve a object use a generic method for all tables (which returns a base object). And then you can check the type of the returned object to get the A,B,C object.
TableBase
Id
TableB (1):TableBase
TableC (2):TableBase
TableD (3):TableBase

Need help with Entity Framework Relationshipships

I'm new to Entity Framework and just experimenting...
Consider 3 db tables where Person is a base table. I want the Employe table to enherit from Person, storing employee specific info. It seems that EF requires that PersonId also be the PK of the Employee table, so I made EmployeeID a unique index.
Next I added a table, Application, which stores one record for every software application that the Employee supports, creating a foreign key from Application.EmployeeId to Employee.EmployeeId.
However, EF doesn't seem to recognize relationships that involve unique indexes, but only Primary Keys.
What I can do is create a relationship from Application.PersonId to Person.PersonId, however, only Employees can be responsible for an Application, so it seems more natural to me to have Application as a "child" of the Employee table rather than the Person table.
Is this possible in EF?
You can build your relation between Employee (PersonId) and Application (EmployeeId). In such case the integrity should work as you expect because only PersonIds in Employee table will be only for existing employees. EF has currently no support for unique keys.