1

My Tables

CREATE SEQUENCE SeqA START WITH 1 INCREMENT BY 1;
CREATE TABLE TblA (
    CompanyID BIGINT NOT NULL,
    ID BIGINT DEFAULT (NEXT VALUE FOR SeqA),
    ..
    PRIMARY KEY (CompanyID, ID));

And

CREATE TABLE TblB (
    MyID BIGINT PRIMARY KEY IDENTITY(1, 1),
    ..
    CompanyID BIGINT NOT NULL, -- These are foreign key references to TblA
    ID BIGINT NOT NULL
);

Question 1: How can I setup foreign key to reference TblA from TblB?

Question 2: When using EF Core 3.1, how can I setup navigation property for CompanyID and ID in TblB to references TblA?

s k
  • 4,342
  • 3
  • 42
  • 61
  • What's the point of having a composite key? `ID` alone is unique. Adding `CompanyID` dooesn't help at all. Quite the oppositte - it allows inserting *duplicate* ID values by specifying a different `CompanyID`. This would make sense in a many-to-many table. Is this what `tblA` is? – Panagiotis Kanavos Sep 22 '20 at 12:15
  • I have the same application deployed in different PCs supporting different companies. At the end of the day, the data need to be merged. – s k Sep 22 '20 at 12:17
  • You can specify an anonymous type combining multiple properties both in `HasKey` and `HasForeignKey`. Check [Composite Key as Foreing Key](https://stackoverflow.com/questions/5436731/composite-key-as-foreign-key). – Panagiotis Kanavos Sep 22 '20 at 12:20
  • Thanks. I will give it a try. – s k Sep 22 '20 at 12:30

0 Answers0