EntityFramework Core - Related Entities without specific foreign key

49 Views Asked by At

thanks for taking the time to look over my question.

I am trying to get some data along with the related records from a database using a DbContext. I already have an example of this setup for one of my relationships -

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<FinancialTransaction>()
                .HasOne<LedgerEntry>(e => e.ledger_entry)
                .WithOne(t => t.financial_transaction)
                .HasForeignKey<LedgerEntry>(e => e.financial_transaction_id);
        }

However, I also have a table which has loose references to other records, using a combination of source_id and source_type columns. Source_id is the primary key of the other record, and source_type is the singular name of the other record type (such as "account", "user" etc.).

Is anyone aware of a way to set this up using EntityFramework, similar to my example above? Any advice is appreciated and if you need any more information, please ask :)

Thanks!

0

There are 0 best solutions below