Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map to customized (dynamically generated but with same structure) tables entity framework core #15753

Closed
hnviradiya opened this issue May 19, 2019 · 12 comments

Comments

@hnviradiya
Copy link

hnviradiya commented May 19, 2019

Can't there be a mapping of multiple tables (dynamically N tables) to same class.

I have multiple tables generated at runtime. I want to query them. What is a solution to this?

Cant create classes. As tables are generated at runtime.

E.g. table_1, table_2. table_3, ...table_N should map to "table" class. To which I should be able to provide parameter that which table to query on.

Currently onmodelcreating we are using fixed string method named "toTable". Where we can provide fixed string of table name we want to map to. It would be great if we can provide this dynamically.

there are multiple requirements like this on stackoverflow for which people are not getting answered for as per requirement of OP.

https://stackoverflow.com/questions/56135928/map-to-customized-dynamically-generated-but-with-same-structure-tables-entity/56202714#56202714

https://stackoverflow.com/questions/38372618/multiple-tables-with-same-structure-entity-framework

https://stackoverflow.com/questions/31035238/dynamic-table-name-with-entity-framework

@ajcvickers
Copy link
Member

@hnviradiya Can you explain a bit more how you would use this kind mapping? For example, what would be the code you would write for a query against "table_3"? Also, would all tables have the same columns? If not, how would this be handled? (I'm asking these questions to try to distill down to what EF would need to support for this to work.)

@hnviradiya
Copy link
Author

hnviradiya commented May 20, 2019

All tables have same db structure (basically this is a partition of own implementation to handle large datasets).
I can write something like
dbcontext.DbSet("TableName") which will map TableType to TableName mapping..???

So if I want to query against "table_3", I can write as "dbcontext.DbSet<Table>("table"+ passedId)"

isn't this possible?

@ajcvickers
Copy link
Member

@hnviradiya So then what is the CLR type Table and how does it relate to this?

@hnviradiya
Copy link
Author

Table is a model (not dbset, but model). because all tables have same structure we can use it in generic way?

@ajcvickers
Copy link
Member

@hnviradiya Okay, so you do have a class that you want to map, you just want to dynamically change which table it maps to. Is this correct?

@hnviradiya
Copy link
Author

yes :)

@sdanyliv
Copy link

Generating query for such tables is not a problem. But it also needs ChangeTracker extension point.
Also user may require Database and Linked Server support also. This is a typical situation for projects with a history when CodeFirst is not an option at all.

@hnviradiya, i suggest to tryout this extension 👽
And you will be able to write such queries:

var items = context.DbSet<Table>().ToLinqToDBTable().TableName("table_3")
  .Where(...)
  .ToList();

For sure ChangeTracker will be not availaible but CRUD operation can be performed in other way:

var affected = context.DbSet<Table>().ToLinqToDBTable().TableName("table_3")
  .Where(...)
  .Set(v => v.Field, someValue)
  .Update();

@hnviradiya
Copy link
Author

Great, that will work for me. Thanks :)

@ajcvickers
Copy link
Member

This is essentially a duplicate of #15671

@xiaodongcool
Copy link

What's the best course of action now ? The sdanyLIV scheme above is now not working ?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@dvora2000
Copy link

Hi
I have similar task with dynamic tables in entity framework.
My problem is how to craete those tables.
The table has to be created when client user send string to api.
the table name is the string. and columns are static.
Is it possible to do that in entity framework? how?
thanks

@jonathanlocke
Copy link

I also have this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants