question

Rozemyne-8093 avatar image
0 Votes"
Rozemyne-8093 asked ZhiLv-MSFT commented

Getting Values from EF Core Context with Dynamic Calls to Tables

I'm working in EF Core 5 with Razor pages connecting to SQL Server and am trying to develop a generic form that handles multiple lookup tables. So I'm trying to dynamically access the tables in my context. My problem is that I can access the Properties (column names) of the tables themselves but I can't access the keys of the IEnumerable object when I pass it. I have a dictionary of all the tables that passes the context to the form. I can see the data is there along with the keys. How do I reach the keys (specifically CenterTypeId and CenterType in the images provided) so I can get my data without having to pass a select in my context dictionary? Some of the tables I'm going to be using have more than 2 columns and I'll need access to all the data in them. The only properties in the dbSet IEnumerable variable that I am able to access are the Entity Type and the Tracking.LocalView

172722-code.png

172669-values.png


dotnet-entity-framework-coredotnet-aspnet-core-razor
code.png (96.5 KiB)
values.png (154.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Rozemyne-8093,

First, the NullReferenceException is very clear, the collnfo is null.

173022-image.png

Second, I'm a little confused about your code, from your description, it seems that you have already got the column name, and the dbset is a dataset to store the data, right? If that is the case, to get the value from the dbset, why using the above code to get the type and property, why not directly using LINQ to DataSet to get the data. You can also add a select clause to filter the data based on the column name.

Best regards,
Dillion

0 Votes 0 ·
image.png (13.1 KiB)

0 Answers