I have a forum of this model:

Topic
- Id
- Title
- Text
- LastMessageId (nullable)

Message
- Id
- Text
- TopicId

I need to populate a data table in Framework Core 2.1 with default values ​​using the OnModelCreating in the ApplicationDbContext using the _builder.Entity<>().HasData function.

But there is a problem.

In the topic there are messages. Each message belongs to one topic. But the topic also has a record of the last message LastMessageId .

When I try to fill in the test data, the error crashes:

It was a message that was saved: 'Topic [Added] <- Messages Topic {' TopicId '} Message [Added] <- LastMessage {' LastMessageId '} Topic [Added]

at at Microsoft.EntityFrameworkCore.Internal.Multigraph`2.ThrowCycle ...

It is clear that the cycle is that the message has a parent topic, and the topic has the last message.

How to fill in such data?

    0