Skip to content

How to add tenancy to inherited models #17

@pwfraley

Description

@pwfraley

I am having a problem I can't seem to solve (I am by no way a c# expert)

I have a abstract base entity called BaseContact
From this BaseContact I inherit PhoneContact and EmailContact.

If I try to add the Tenancy to PhoneContact and EmailContact I get the error that filters can only be added to the BaseEntity. But if I only add the Tenancy to the BaseEntity my application crashes trying to add a Phone- or a EmailContact with the message tenantId can not be null.

How do I go about adding tenancy to inherited models?

This Compiles, but crashes upon Creation of inheritated models:

            modelBuilder.Entity<BaseContact>(
                ctx =>
                {
                    ctx.ToTable("Contact");
                    ctx.HasIndex(ct => ct.Id);
                    ctx.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: true);
                    ctx.HasQueryFilter(ct => ct.TenantId == _tenancyContext.Tenant.Id);
                }
            );

            modelBuilder.Entity<PhoneContact>().HasBaseType("BaseContact");
            modelBuilder.Entity<EmailContact>().HasBaseType("BaseContact");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions