简体   繁体   中英

TableName (Contains / Regex) When Creating An Entity - Entity Framework - Table Prefix

I'm trying to create an EF model based on an existing MySQL Database. Upon creating an entity, I map it using:

[Table("databaseName.tableName")] or:

modelBuilder.Entity<Lang>().ToTable("tableName") in OnModelCreating .

The issue I have is that the table names could have an unexpected prefix . What I want to do is, without knowing the prefix, search for any table in the database that:

(Something like) EndsWith("tableName") use it, or:

which name matches a Regex .

Is this doable? The alternative would be to ask the user for the TablePrefix specific to his/her database and work with it ( modelBuilder.Entity<Lang>().ToTable(prefix + "tableName" ) but that would be quite inconvenient. Since the prefix is the only thing that can change from database to an other, it would be really helpful to be able to detect tables by their name.

You can't query the database in OnModelCreating, but you can use the Connection String to open an ADO.NET Connection to the database and run a query to retrieve the table names and construct a mapping between the Entity types and the table names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM