Dynamically Selecting Data from Table in Entity Framework - entity-framework

I know this question might have been asked before but I have not found a single answer yet.
Basically I am using entity framework and I am in need of selecting data from a database without knowing the name of the table, as this will be generic.
Now if I do not know the table name, I do not know the type too as I have tried using context.Database.SQLQuery or context.Database.ExecuteSQLStatement but these all require the type of the object it should be expecting.
All I am receiving as parameters are the name of the table and the row ID.
Could anybody give me further advice?
Thanks.
#
Edit:
I have just been notified that the only property I would need from this table is the Name field...

Related

How to query a parent table and inherited child table together in one query

I am using go and pq to interface with my postgres database.
I have a simple user table which has basic fields. Id, name, type. My auxillary table, admin inherits from user and adds it's own field panel, and another one that is owner and adds owner. Whether that be using table inheritance, or a supporting table.
My question is if I hit and endpoint that points to user/1 at this point I don't know what type of user this person is yet here. I know we can use jwts and other ways to provide this from the front end. I'm more curious about if there is a way to figure out the user and it's type and query the additional fields in one query?
Ie. I hit the endpoint I would Select from users, get the type, then use that type to get the additional fields. So I would effectively be doing two queries on two tables to get the complete data. Is there a better solution of doing this? Is there some optimizations I could do.

Rails 5.1.2 - Single Table Inheritance: No migration is getting generated

I am trying to generate scaffolding for STI implementation. I issue the following.
rails g scaffold user1 type name email
rails g scaffold member company subscription --parent user1
Every thing gets generated file except for the migration file my 'member' model.
When I try to create a member record like this
Member.create(name: "My Name", email: "myname#example.com",
company: 'Example LLC', subscription: 'Monthly Gold' )
I get this error:
ActiveModel::UnknownAttributeError: unknown attribute 'company' for Member. from (irb):1
Any ideas on what is going on?
I use rails 5 and db is postgres
The --parent option assumes that you are already all setup for single table inheritance, i.e. the parent class has a table with a type column (or whatever column you are using for this).
Since the model will be stored in the parent's table, there is no need to create a new table for the subclass, hence no migration
I got this answer similar to this question asked by someone.
To my understanding, you are on the wrong track. In single table inheritance, all the attributes must be present in the parent model table with an additional column name 'type' to indicate the type of inherited model. The column name 'type' can be changed with appropriate settings but ActiveRecord by default looks for 'type' column. You are getting 'UnknownAttributeError' error cause the parent model does not have the following column in its table. You need to write a migration to add the new columns. Hope you understand the concept of STI. For further exploration, I am providing you the link of the official guide. Hope your problem will be solved.
http://edgeguides.rubyonrails.org/association_basics.html#single-table-inheritance

How to use Entity framework to pass Table Valued parameter to Stored procedure

I have tables with 2 level hierarchy, Parent->Child->GrandChild
I have create stored procedure with three table valued input parameter ParentTable, ChildTable, GrandChild Table.
Now, I want to consume it in .net using entity framework.
Solution all over internet is, create DataTable in .net , store data in it and pass the same as parameter in stored procedure.
But, I want to use entities instead of data table as data is stored in entity objects. Please suggest. Many Thanks.
You need to have look at this question
Create data table from entities and then pass it to the stored procedure. I haven't tried the code, just showing you the path to go. I hope it may help, looking for better solution.

Getting Error for updating a row in a table using EF

I am getting this below error when I try to update a row in my table using Entity Framework.
I am able to add a new entry to table but not able to update the existing entry.
Error:-
The specified value is not an instance of type 'Edm.Decimal'\r\nParameter name: value
And my table has all the columns of type (nvarchar,char,bit,numeric,uniqueidentifier,int)
I dont even have a column of type Decimal. I dont know where this is coming from.
I am using ASP.NET MVC3 and Entity Framework. I have checked the table mapping with the Entity Framework and it looks fine.
Please help me.
Thanks,
Vivek
I found the solution. The Identity column of the table was type Numeric. It should have been type Big Int. I updated the type of column and it has worked.
Thanks,
Vivek

Entity Framework Inheritance

I'm new to EF, and trying to work out the best way to do something
I have a procedure that returns the details of a table, but also a calculated value. What I'd like is for it to return this information to an entity that could contain this, whether it's the original entity, or an entity that's based on the original (so that if the original table changes, it will still map). What I'd like to avoid is maintaining 2 entities - 1 for the table, and 1 for the stored procedure results.
Thanks...
You could create a view that contained the table fields as well as the calculated values.