How to change a clustered index in Entity Framework 6.1 Code First model and apply it to an Azure database

后端 未结 4 1011
闹比i
闹比i 2021-01-18 10:31

Using the Entity Framework 6.1 code first model, what is the best way to go about changing the clustered index on a table from the default ID to another set of columns. Azur

4条回答
  •  野的像风
    2021-01-18 11:13

    This is truly an area where EntityFramwork (Core) had to advance and it still is hard.

    So, I could not use IsClustered(false) for my GUID / string Primary keys, for the simple reason, the project having DbContexts was DB - agnostic. So you needed to Add EntityFrameworkCore.SqlServer and IsClustered is available then, and only.

    So, my solution was simple. Add no nuget package but this attribute. This ONLY works on EF Core.

    I have tested this on SQL. Though, not sure if the other providers would allow this string not having any meaning. (e.g. SQLite does not know clustered indexes)

     p.HasKey(k => k.Id).HasAnnotation("SqlServer:Clustered", false);
    

提交回复
热议问题