lenny
lenny

Reputation: 784

Error message for missing relationship definition although it is defined

I am trying to set up a new project with EF Core. To define relationships between my entities I am using the OnModelCreating event:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Article>(article =>
    {
        article.OwnsOne(e => e.Category);
        article.OwnsOne(e => e.CurrentCondition, condition =>
        {
            condition.OwnsMany(e => e.CriteriaConditions, articleCriteriaCondition =>
            {
                articleCriteriaCondition.OwnsOne(e => e.Criteria, criteria =>
                {
                    criteria.OwnsMany(e => e.CriteriaOptions, criteriaOption =>
                    {
                        criteriaOption.OwnsOne(e => e.FollowUp);
                    });
                });
            });
            condition.OwnsOne(e => e.CurrentCategory);
        });
    });

    modelBuilder.Entity<Department>(department =>
    {
        department.OwnsMany(e => e.Categories);
        department.OwnsMany(e => e.CriteriaCatalogues, criteriaCatalogue =>
        {
            criteriaCatalogue.OwnsMany(e => e.Criterias, criteria =>
            {
                criteria.OwnsMany(e => e.CriteriaOptions, criteriaOption =>
                {
                    criteriaOption.OwnsOne(e => e.FollowUp);
                });
            });
            criteriaCatalogue.OwnsMany(e => e.CriteriaCatalogues);
        });
        department.OwnsMany(e => e.Criterias, criteria =>
        {
            criteria.OwnsMany(e => e.CriteriaOptions, criteriaOption =>
            {
                criteriaOption.OwnsOne(e => e.FollowUp);
            });
        });
    });
}

Here is a class diagram to roughly see where I am going with this: Class diagram

Note that a CriteriaCatalogue can contain many other CriteriaCatalogues.

Now when trying to run Database.EnsureDeleted(); I get this error message:

System.InvalidOperationException: 'The entity type 'ArticleCriteriaCondition.Criteria#Criteria' is configured as owned, but the entity type 'Department.CriteriaCatalogues#CriteriaCatalogue.Criterias#Criteria.CriteriaOptions#CriteriaOption.FollowUp#Criteria' is not. All entity types sharing a CLR type must be configured as owned.'

Although I think I did configure my FollowUp correctly, I obviously did not. Can somebody help me out here? I have no idea what to do.

Upvotes: 2

Views: 230

Answers (1)

Rob White
Rob White

Reputation: 1002

As per my comment, I'm not familiar with the syntax you're using for creating relationships (so I don't know the benefits). I've found this way to be simpler to understand but that could be my failing.

I'd setup your relationships individually throughout but for an example. The CurrentCondition model/table. This assumes CurrentCondition has a primary key called Id and a foreign key called CurrentConditionId

modelBuilder.Entity<CurrentCondition>()
            .HasOne(parentCondition => parentCondition.CurrentCondition)
            .WithMany(childCondition => childCondition.CurrentConditions)
            .HasForeignKey(parentCondition => parentCondition.CurrentConditionId)
            .HasPrincipalKey(childCondition => childCondition.Id)
            .OnDelete(DeleteBehavior.NoAction); // specify your deletebehaviour

It doesn't offer an answer to the specific issue. But offers an alternative that might bypass the problem.

Upvotes: 1

Related Questions