This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27075|Reply: 0

[Source] EF Core Series 2 encapsulates OnModelCreating, creates indexes using reflections, and more

[Copy link]
Posted on 2020-9-22 10:57:19 | | | |
Indexing is a common concept across multiple data stores. Although their implementation in a data store can vary, they can also be used for column-based lookups (or a set of columns) to be more efficient.

The hyperlink login is visible.

What to know:

.net/c# takes advantage of the reflection dynamic execution method
https://www.itsvse.com/thread-4771-1-1.html

C# reflection gets the displayname of the object property
https://www.itsvse.com/thread-3219-1-1.html

In Entity Framework 6, you can add indexes by adding attributes to fields, but in ef core, you cannot use attributes to add indexes to table fields, so you must rewrite the OnModelCreating method and write the corresponding rules one by one in it.

Different tables need to be written in this method, which is not easy to maintain and the readability is too poor, so we write the index of each table in our own method to facilitate updates and searches.

ef core is usedcode fristSchema creates a table structure.

First, create a new IOnModelCreate interface, the code is as follows:

We define an object at the bottom of the class class of different tables and inherit the interface, for example:

AccountSetting Code:
EmailSentHistorySetting code:

In your own DbContext context object, rewrite the OnModelCreating method, use the reflection method, and execute the OnModelCreating method in turn to create indexes, relationships, data seeding, etc.

The OnModelCreating method is as follows:



Execute the Create Migration command to generate the following code:



Discovery, indexes can be successfully created through reflection methods, etc.

(End)





Previous:.NET Core implements distributed lock principle parsing based on Redis
Next:ASP.NET Core (1) uses Redis caching
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com