"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

Index is not an attribute class – Error while migrating from ASP.Net MVC 5 to .Net Core

Recently one of my friends was migrating a project from ASP.Net MVC 5 to ASP.Net core 3.1. One of the challenges he faced is with the Index Attribute in data annotations. The .Net Core is not recognizing the Index attribute. When he copied his class from his MVC 5 project, he got the following error message.

clip_image001

Though the other data annotations accepted by .Net Core, this data attribute “Index” was throwing an exception.

“Index is not an attribute class”

Cool!. Let us dig into the details. You can find the related post on the Entity Framework Github page.

https://github.com/dotnet/efcore/issues/1698

https://github.com/dotnet/efcore/issues/4050

From the page, it is clear now, this is not a bug, the EF Core team didn’t migrate the Index Attribute from the EF 6. So we need to live with this.

Now the question arises, how we are going to add the Indexes to our Tables. The answer is to use the Fluent API in ASP.Net core.

You can refer to the following link on the Microsoft Site to get the details of how to apply the Index to a property.

https://docs.microsoft.com/en-us/ef/core/modeling/indexes

So the solution is to replace the Index Attribute to the Fluent API, as below.

clip_image002

In real life, you will have many classes, and adding all classes using Fluent API inside OnModelCreating() method will make your code difficult to maintain. The solution is to create a separate class derived from IEntityTypeConfiguration<TEntity>. This will help us define all the related configurations in a single class and then apply the configurations from the OnModelCreating() method.

Let us apply the configuration class for the above. The following is the configuration class.

clip_image003

Once you have the configuration class, you can apply the configuration in the ModelCreating() method as follows.

clip_image004

Happy Coding!Smile

19 Comments

  • Thanks for Sharing this useful information.

  • Nice Info. Really useful.

  • More about this issue

  • I read the good information. Thank you very much.

  • I'm going to see some good stuff. I will visit you often in the future. Please also visit our blog.<a href="https://to-chelin07.com/">토토사이트 </a>

  • nice to meet you.I have a lot of questions about this site. We will visit you often.

  • <a href="https://megaslotgame.com/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1-ambpoker/ambpoker-%e0%b9%82%e0%b8%9b%e0%b8%a3%e0%b9%82%e0%b8%a1%e0%b8%8a%e0%b8%b1%e0%b9%88%e0%b8%99/">ambpoker โปรโมชั่น</a> รวมโปร ambpoker แจกเครดิตฟรี ทุกวี่วัน รวมทั้ง ก็พิเศษสุดๆเอาไว้ต้อนรับสมาชิกทุกคน <a href="https://megaslotgame.com/">MEGASLOTGAME</a> ไม่ว่าจะเป็น โปรโมชั่น pg สมัครเป็นสมาชิกใหม่รับ เครดิตฟรี50 หรือเป็นโบนัส

  • [url=https://megaslotgame.com/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1-ambpoker/ambpoker-%e0%b9%82%e0%b8%9b%e0%b8%a3%e0%b9%82%e0%b8%a1%e0%b8%8a%e0%b8%b1%e0%b9%88%e0%b8%99/]ambpoker โปรโมชั่น[/url] ambpoker แจกเครดิตฟรี ทุกวี่วัน รวมทั้ง ก็พิเศษสุดๆเอาไว้ต้อนรับสมาชิกทุกคน[url=https://megaslotgame.com/megaslotgame/]MegaSlot[/url]ไม่ว่าจะเป็น โปรโมชั่น pg สมัครเป็นสมาชิกใหม่รับ เครดิตฟรี50 หรือเป็นโบนัส

  • [url=https://megaslotgame.com/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1/%e0 %b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1-ambpoker/ambpoker-%e0%b9%82%e0%b8% 9b%e0%b8%a3%e0%b9%82%e0%b8%a1%e0%b8%8a%e0%b8%b1%e0%b9%88%e0%b8%99/]ambpoker Promotion [/url] ambpoker gives away free credit every day, including very special to welcome all members [url=https://megaslotgame.com/megaslotgame/]MegaSlot[/url] whether it is a pro Motion pg Sign up as a new member get 50 free credits or as a bonus

  • <a href="https://megaslotgame.com/%e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1/ %e0%b8%9a%e0%b8%97%e0%b8%84%e0%b8%a7%e0%b8%b2%e0%b8%a1-ambpoker/ambpoker-%e0%b9%82%e0% b8%9b%e0%b8%a3%e0%b9%82%e0%b8%a1%e0%b8%8a%e0%b8%b1%e0%b9%88%e0%b8%99/">ambpoker Promotion</a> includes pro ambpoker, giving away free credit every day, including a very special welcome to all members <a href="https://megaslotgame.com/">MEGASLOTGAME</a> whether Will it be a pg promotion, sign up as a new member, get 50 free credits or as a bonus

  • It's very interesting. And it's fun. This is a timeless article. I also write articles related to , and I run a community related to <a href="https://www.iflytri.com/">메이저사이트</a>. For more information, please feel free to visit !!
    FCH

  • it’s an amazing post and I learned a few tips worthy of keeping :). Thanks for sharing those and keep up the amazing work!

  • Excellent starter guide for Beginners! Thanks for putting your effort in putting out this great post.

  • I've seen some very helpful stuff. I found a lot of information, but your article was the most attractive and excellent. I will visit you often.

  • Really grateful information this gonna helpful stuff

  • Yt1s is a Great website for converting Youtube to Mp3 conversion if you want to download youtube videos to your devices. follow below steps

    First Open website of Yt1s - https://yt1s.mobi/

    Copy Youtube Video and Paste it into search box

    Press the Download button and download start fast and easily

  • Must be appreciated this article. Really Guys!

  • This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.</a> I would like to recommend a good topic if you wrote it. I'll leave my blog address below, so come and check it out. <a href="https://totomeoktwiblog.com/" target="_blank">메이저놀이터</a>

  • I was very impressed by your post. I want to write an article on your topic, but please visit my blog later.<a href="https://totoblogs.com/" target="_blank">먹튀검증 </a>

Add a Comment

As it will appear on the website

Not displayed

Your website