How can I create a relationship in EF core where the child(FullName) table has DrugId which is the primary key. In the parent table(Basic Data), DrugId is foreign key. But there are other child tables and they are also referring DrugId in the parent table as a foreign key. How can I create this relationship in C# ef core? One way is to create one dedicated foreign key column for every child table but it will make duplicate DrugId in the parent table. for example:
Parent Table:
public int DrugId {get;set;}
public int FullNameDrugId {get; set;}
public int DataInfoDrugId {get; set;}
public int FormCode {get;set;}
FullName Table:
public int DrugId {get;set;}
public string name {get;set;}
DataInfo Table:
public int DrugId {get;set;}
public string Size {get;set;
In the parent table, DrugId and FullNameDrugId, and DataInfoDrugId are the same.
I want to get data from the parent id using LINQ and include the other two tables just adding "Include" statement. I want to navigate from parent table to child. Now how can I deal with this?
2
Answers
That looks like a Many-To-One relationship where several BasicData could refer to the same Drug, where the relationship between Drug and other tables like FullName that share the DrugId as a PK would be a One-to-One.
Using explicit mapping for the relationships:
// Drug (EF Core)
// Basic Data
By default when using
HasOne()
.WithOne()
it will join on the PKs of both tables.Then to get to the drug’s full name from a given BasicData query:
… To get the idea of how to leverage the relationship. If loading the entities themselves then eager load using
.Include(x => x.Drug).ThenInclude(x => x.Fullname)
(Assuming EF Core)Well, I would suggest using PKs to reference the other one-to-one related tables so your tables and relation configuration should look like the below:
And you can configure them like below :