MySQL with EntityFramework Core - Composite Foreign Key

China☆狼群 提交于 2021-02-11 14:19:30

问题


I have two tables Project and ProjectMedia. they are defined as follow:

CREATE TABLE `project` (
  `ID` int(11) NOT NULL,
  `LANG` char(2) NOT NULL,
  `NAME` varchar(64) CHARACTER SET utf8 DEFAULT NULL,
  `DESCS` varchar(2048) CHARACTER SET utf8 DEFAULT NULL,
  `SHORT_DESC` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`ID`,`LANG`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ID and LANG are a composite primary key in the Project table.

ProjectMedia table definition:

CREATE TABLE `project_media` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `PATH` varchar(1024) DEFAULT NULL,
  `TYPE` char(1) DEFAULT NULL,
  `IS_LOCAL_FILE` bit(1) DEFAULT NULL,
  `PROJECT_ID` int(11) NOT NULL,
  `LANG` char(2) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `PROJECT_MEDIA_PROJECT_PROJECT_ID_LANG_idx` (`PROJECT_ID`,`LANG`),
  CONSTRAINT `PROJECT_MEDIA_PROJECT_PROJECT_ID_LANG` FOREIGN KEY (`PROJECT_ID`, `LANG`) REFERENCES `project` (`ID`, `LANG`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I have added two columns into ProjectMedia PROJECT_ID and LANG and I have created a composite foreign key for them to reference them into project table

but when I execute scaffold command as follow: I am getting this message

dotnet ef dbcontext scaffold "server=localhost;database=mydb;user=root;" "MySql.Data.EntityFrameworkCore" --output-dir "Persistence" --context "MyDbContext" --force

Could not scaffold the foreign key 'mydb.project_media(PROJECT_ID)'. A key for 'ID' was not found in the principal entity type 'Project'. Could not scaffold the foreign key 'mydb.project_media(LANG)'. A key for 'LANG' was not found in the principal entity type 'Project'.

What should I do?

来源:https://stackoverflow.com/questions/53842449/mysql-with-entityframework-core-composite-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!