Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Table name Mapping by Entity Framework Core

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you Entity Framework Core how to achieve table name mapping, I hope you will learn something after reading this article, let's discuss it together!

Table name mapping

We know that by default, the name of the table generated using EFCore Code First is the same as the name of the entity attribute defined in the data context class, for example:

Public DbSet Bloges123 {get; set;}

The attribute name defined here is Bloges123, so the final table name generated in the database is also called Bloges123. Look at the test below.

We first add a migration, and each migration generates a corresponding migration record class, as shown in the following figure:

As you can see, it shows that the name of the created table is Bloges123. Finally, update the database, and check the table name generated by the database after the update is complete:

You can see that the final generated table name in the database is the name of the configured entity attribute. What if we don't want to use automatically generated table names? At this point, you need to do the table name mapping and set the name of the last generated database table in the code.

If you want to map, you need to override the OnModelCreating method of the parent class in the data context class. There is a method ToTable () in which the parameter is the name of the table you want to generate, as shown in the following figure:

The modified data context class code is as follows:

Using EFCore.Model;using Microsoft.EntityFrameworkCore;namespace EFCore.Data {/ data context / public class EFDbContext: DbContext {protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) {optionsBuilder.UseSqlServer ("Data Source=.;Initial Catalog=EFTest;User ID=sa;Password=123456;");} public DbSet Blogs {get; set } / override the OnModelCreating method, configure mapping / protected override void OnModelCreating (ModelBuilder modelBuilder) {/ / configuration table name mapping modelBuilder.Entity () .ToTable ("Blog"); base.OnModelCreating (modelBuilder);}

After this setup is complete, we will use data migration to generate the database this time and see the final result. Add the migration record class generated after the migration:

You can see: the migration record class shows that the generated table name is our own configured table name, and then update the database to view the database table name:

This completes the table name mapping, resulting in our own configured table name.

Note: there is a limit when setting table names: the maximum length of table names is 128.

We modify the code to set the table name length of more than 128 to see the effect:

Var tableName = string.Join (", Enumerable.Repeat (" t ", 250,250) .ToArray (); modelBuilder.Entity () .ToTable (tableName)

Use migration to generate the database view effect:

As you can see, an error was reported while updating the database, indicating that the maximum length was exceeded.

After reading this article, I believe you have a certain understanding of "how to achieve table name mapping in Entity Framework Core". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report