Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using code first for create my database with an ASP.NET MVC project, but I didn't create index for columns. Now I want to index a few columns in my existing database, I can do it from sql server management studio.

I want to ask this, should I make any changes in my models which I created the database, or is it enough to do this operation only on the database?

Thanks a lot.

What I have tried:

I need information about it, I couldn't find good example.
Posted
Updated 22-Apr-20 19:09pm

In the migration, You should add your index creation to the up() method and a drop statement to the Down() method to enable downgrading the database schema
C#
namespace TestLib.Migrations
{
    using System.Data.Entity.Migrations;
 
    public partial class UserTable : DbMigration
    {
        public override void Up()
        {
            CreateIndex("User", new string[] { "UserId", "UserName" },
                true, "IX_User_UserId");
        }
 
        public override void Down()
        {
            DropIndex("User", "IX_User_UserId");
        }
    }
}

Then you should apply the update through the Package Manager Console
 
Share this answer
 
You should add them to your code so they exist if the database is re-made from the code.

Indexes - EF Core | Microsoft Docs[^]

Code first, in general, is a bad idea for anything "serious". It's ok for quick prototyping or getting something started, but not really for any production system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900