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?

3
  • 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. Commented Aug 21, 2019 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? Commented Aug 21, 2019 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 which does not require key, and ToView(name) fluent API (the name can be table name). Commented Aug 21, 2019 at 13:07

0

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.