2

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.

1
  • The syntax you're using I'm not familiar with, I don't know the full ins and outs of fluent but I'll post a different option which, for me at least looks easier. Might help, might not. Commented Dec 21, 2020 at 22:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

your answer nudged me into the right direction... my issue was, I first saw the OwnsOne method, and started using it. owned types being somewhat special in EF started causing me a bunch of issues, resulting in a weird, nested syntax. What I use now is HasOne and HasMany, which seems to be working just fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.