question

KrzysiekP-3518 avatar image
0 Votes"
KrzysiekP-3518 asked JackJJun-MSFT commented

EF Core correct model design

I'm trying to set the model as below,

      public class Person
         {
             [Key]
             public int PersonId { get; set; }
             public string Name { get; set; }
        
             public List<Person> Friends { get; set; }
             public List<Person> Enemies { get; set; }
         }

But by default, ef core does not create such relations as I would like,

Default ef core create two tables:

 CREATE TABLE `persons` (
   `PersonId` int(11) NOT NULL AUTO_INCREMENT,
   `Name` longtext,
   PRIMARY KEY (`PersonId`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

and

 CREATE TABLE `personperson` (
   `EnemesPersonId` int(11) NOT NULL,
   `FriendsPersonId` int(11) NOT NULL,
   PRIMARY KEY (`EnemesPersonId`,`FriendsPersonId`),
   KEY `IX_PersonPerson_FriendsPersonId` (`FriendsPersonId`),
   CONSTRAINT `FK_PersonPerson_Persons_EnemesPersonId` FOREIGN KEY (`EnemesPersonId`) REFERENCES `persons` (`PersonId`) ON DELETE CASCADE,
   CONSTRAINT `FK_PersonPerson_Persons_FriendsPersonId` FOREIGN KEY (`FriendsPersonId`) REFERENCES `persons` (`PersonId`) ON DELETE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

I would expect three tables

How can I solve this problem



dotnet-entity-framework-core
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KrzysiekP-3518 , What is your desired the third table? Could you describe it in more detail?

0 Votes 0 ·

0 Answers