Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CreatorId and TenantId not autofilled on creation in tenants #1360

Closed
Silthus opened this issue Jun 24, 2019 · 5 comments · Fixed by #1369
Closed

CreatorId and TenantId not autofilled on creation in tenants #1360

Silthus opened this issue Jun 24, 2019 · 5 comments · Fixed by #1369
Labels
Milestone

Comments

@Silthus
Copy link
Contributor

Silthus commented Jun 24, 2019

I followed the tutorial and created my first own FullAuditedAggregateRoot entity named Parkingplace. The entity also implements IMultiTenant to provide multi tenant support.

public class Parkingplace : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        public const int MaxParkingplaceName = 128;

        public Guid? TenantId { get; set; }

        public string Name { get; protected set; }

        public ParkingplaceType Type { get; protected set; }

        public Guid OwnerId { get; protected set; }

        protected Parkingplace()
        {
        }

        public Parkingplace(string name, ParkingplaceType type)
        {
            Name = name;
            Type = type;
        }
    }
        public static void ConfigureParkSucht(this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            /* Configure your own tables/entities inside here */
            builder.Entity<Parkingplace>(b =>
            {
                b.ToTable(ParkSuchtConsts.DbTablePrefix + "Parkingplaces", ParkSuchtConsts.DbSchema);
                b.ConfigureFullAuditedAggregateRoot();
                b.ConfigureMultiTenant();

                b.Property(parkingplace => parkingplace.Name)
                    .IsRequired()
                    .HasMaxLength(Parkingplace.MaxParkingplaceName);
                b.HasIndex(parkingplace => parkingplace.Name);
            });
        }

I went ahead and created the matching Dtos and an AppService for the entity. But I have the problem that the CreatorId and TenantId are not set automatically when I create the entity in a tenant.

If I use the host (default) tenant the CreatorId ist set correctly. The TenantId remains null which is expected.

image

Do I need to configure the entity somewhere else?

@Silthus Silthus changed the title CreatorId and TenantId not autofilled on create CreatorId and TenantId not autofilled on creation in tenants Jun 24, 2019
@maliming
Copy link
Member

You need to manually set the TenantId for your entity.

@Silthus
Copy link
Contributor Author

Silthus commented Jun 25, 2019

Ok, but why?

There is code in the AsyncCrudAppService base class that should try to set the TenantId:

if (entity is IMultiTenant && !HasTenantIdProperty(entity))

And here the TryToSetTenantId:

protected virtual void TryToSetTenantId(TEntity entity)

For debugging purposes I also checked if CurrentTenant is set and if the conditions inside the TryToSetTenantId are met. And to me it seems like everything should work and the tenant id should be set.

But it isn't.

@hikalkan
Copy link
Member

Did you inherit from AsyncCrudAppService.cs? If not, you should set it yourself. If you inherited, try to override TryToSetTenantId and debug to see it.

protected override void TryToSetTenantId(TEntity entity)
        {
            var tenantId = CurrentTenant.Id;

            if (!tenantId.HasValue)
            {
                return;
            }

            var propertyInfo = entity.GetType().GetProperty(nameof(IMultiTenant.TenantId));

            if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
            {
                propertyInfo.SetValue(entity, tenantId, null);
            }
        }

@Silthus
Copy link
Contributor Author

Silthus commented Jun 25, 2019

I found the issue:

if (entity is IMultiTenant && !HasTenantIdProperty(entity)) should be if (entity is IMultiTenant && HasTenantIdProperty(entity))

If it is ok I will create a PR with a test checking the bug and then fix it.

@maliming
Copy link
Member

@Silthus You are correct, welcome to contribute PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants