I'm starting with ASP.net Core
and I'm creating my models.
I just have created two: one called dto
which will be the base for all my database models, and look like this:
public abstract class Dto
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[Clustered]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime LastUpdate { set; get; }
}
the other one is called subscription
and it look like this:
public class Subscription:Dto
{
public string Name { get; set; }
public decimal Price { set; get; }
public string MeliApiKey { get; set; }
public int MaxQueries { get; set; }
public int UsedQueries { get; set; }
}
I have the following configuration on the Fluent API
:
modelBuilder.Entity<Dto.Dto>().Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()");
modelBuilder.Entity<Dto.Dto>().Property(x => x.LastUpdate).HasComputedColumnSql("SYSUTCDATETIME()");
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
foreach (var prop in entity.GetProperties())
{
var attr = prop.PropertyInfo?.GetCustomAttribute<ClusteredAttribute>(true);
if (attr != null)
{
var index = entity.AddIndex(prop);
index.IsUnique = true;
index.SqlServer().IsClustered = true;
}
}
}
I have added only subscription
as a dbset
because I do not want Table Per Hierarchy inheritance with dto
.
When I run add-migration, I get the following error:
"The index {'CreatedAt'} cannot be added to the entity type 'Subscription' because an index on the same properties already exists on entity type 'Dto'"
Now I have googled the error but I don't see anywhere how to solve this.
Should I remove the dto
class and add the same properties to every object that will represent my model?