0

I have a DTO, let say Vehicle and it has a manufacturer date property (DateTime Vehicle.ManufacturerDate {get; set;}.

When a DTO has a primary key, I would write something like below:

            modelBuilder.Entity<Vehicle>()
                    .HasKey(nameof(Vehicle.ManufacturerDate));

However, in this case, the Vehicle DTO does not have a primary key. Instead, it has a clustered non-unique index on Manufacturer Date.

Is clustered non-unique index same as non-clustered index?

I have searched in Google & SO. The closest post I got is: How to create a Clustered Index with Entity Framework Core

However, that post only answers clustered index on Person.UserName How do I create clustered non-unique index on Vehicle.ManufacturerDate?

hunterex
  • 565
  • 11
  • 27
  • EF Core does not accept entity w/o primary/alternate key. So you have to define such. Also if you are mapping to existing database as it seems, you don't need to define indexes and index attributes (clustered etc.) because these are used only when you use code first model and migrations for maintaining the database schema. – Ivan Stoev Aug 21 '19 at 11:38
  • @IvanStoev, yes you are right. EF Core does not generate table code when I use `Add-Migration` as there is no primary key. Can I set composite key for all columns instead? – hunterex Aug 21 '19 at 12:51
  • I guess you can. But note than you won't be able to edit the record once it's created (inserted). That's because EF Core also does not allow modifying the key. Again, if you are mapping to existing database and you need this "entity" just for reading, consider mapping it as [query type](https://learn.microsoft.com/en-us/ef/core/modeling/query-types) which does not require key, and `ToView(name)` fluent API (the name can be table name). – Ivan Stoev Aug 21 '19 at 13:07

0 Answers0