1

This is my entity class :

public class TenantUsageLog
{
    [Key]
    public Guid Id { get; set; }

    public Tenant Tenant { get; set; }

    [DataType(DataType.Date)]
    public DateTime LogDate { get; set; }

    public int DatabaseRowCount { get; set; }

    public int DailyTraffic { get; set; }

}

I want to create a unique constraint with Tenant object and LogDate

I am adding this code on ApplicationDbContext>OnModelCreating :

       builder.Entity<TenantUsageLog>().HasIndex(p => new { p.Tenant.Id, p.LogDate }).IsUnique();

It returns this error during add-migration :

The properties expression 'p => new <>f__AnonymousType5`2(Id = p.Tenant.Id, LogDate = p.LogDate)' is not valid. The expression should represent a simple property access: 't => t.MyProperty'. When specifying multiple properties use an anonymous type: 't => new { t.MyProperty1, t.MyProperty2 }'. (Parameter 'propertyAccessExpression')

I have also tried this :

       builder.Entity<TenantUsageLog>().HasIndex(p => new { p.Tenant, p.LogDate }).IsUnique();

This time,it returns with this error :

The property 'TenantUsageLog.Tenant' is of type 'Tenant' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

So,could you please help me to handle this problem ? I want to create a unique constraint which helps me to not add same data contains Tenant.Id and LogDate.

Thanks.

1
  • 1
    There should be an overload of HasIndex that accepts string array as property names. You can use that overload and you may specify shadow properties. It should work on later versions of EF core but not so sure about 3.1. You can give it a try. Commented Feb 9, 2023 at 8:39

1 Answer 1

1

Expose the tenant id directly and use it:

public class TenantUsageLog
{
    [Key]
    public Guid Id { get; set; }

    public Guid TenantId { get; set; }

    public Tenant Tenant { get; set; }

    [DataType(DataType.Date)]
    public DateTime LogDate { get; set; }

    public int DatabaseRowCount { get; set; }

    public int DailyTraffic { get; set; }

}
builder.Entity<TenantUsageLog>().HasIndex(p => new { p.TenantId, p.LogDate }).IsUnique();

Also you can try using HasIndex overload accepting string params and pass the column names:

builder.Entity<TenantUsageLog>().HasIndex("TenantId", "LogDate").IsUnique();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works, but I hope my team lead except this,because He does not want to use Id, he wants to use object for foreign key relations.
@TomasShelby added another possible approach. Also you can add index via direct SQL for example by manually changing the migration.
This is : builder.Entity<TenantUsageLog>().HasIndex("TenantId", "LogDate").IsUnique(); better solution for me , because I do not need to expose tenantId. It is tried and worked fine. Thanks a lot.

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.