Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET entity
Hello everyone!
Can you get me a little example hoe to select only columns names from table with EntityFramework?
Posted 10-Apr-13 0:21am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have several possibilities according to your needs. Please see following:
a) Create a view or stored procedure over INFORMATION_SCHEMA.COLUMNS system table, include that view or sp in your EF model.
b) If you want to get the fields of an entity already in the model, just use reflection.
c) But you can also query EF metadata too, check this: http://www.scip.be/index.php?Page=ArticlesNET24[^]
  Permalink  
v2
Comments
Junoli at 10-Apr-13 5:41am
   
I found here the code:
var query = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace)
where meta.BuiltInTypeKind == BuiltInTypeKind.EntityType
select (meta as EntityType).Name;
What is DataSpace? Is name of table? And why here is use condition WHERE?
Can you comment this code. please
Junoli at 10-Apr-13 5:45am
   
Sorry, i added using System.Data.Metadata.Edm;
Junoli at 10-Apr-13 5:47am
   
This example is fine:
 
var result = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace)
where meta.BuiltInTypeKind == BuiltInTypeKind.EntityType
select (meta as EntityType).Name;
 
but ut returns me names of tables, but i need returns names of table
Zoltán Zörgő at 10-Apr-13 5:48am
   
You need names of columns. Check Example 3 on the linked page.
Junoli at 10-Apr-13 5:55am
   
Thank you. This is example return all information about tables. But how to point that return info from table_1?
var result = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace)
.Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType)
let m = (meta as EntityType)
let properties = m.Properties
select new
{
EntityName = m.Name,
MembersCount = m.Members.Count,
KeyMembersCount = m.KeyMembers.Count,
PropertyNames = from p in properties
select new
{
p.Name

}
};
Zoltán Zörgő at 10-Apr-13 6:02am
   
Use Example 4 and add a where condition to from p in (meta as EntityType).Properties to filter on p.DeclaringType.Name
Junoli at 10-Apr-13 6:59am
   
from p in (meta as EntityType).Properties Where p.DeclaringType.Name == "Table_1"?
Zoltán Zörgő at 10-Apr-13 7:31am
   
yes.
Zoltán Zörgő at 10-Apr-13 13:06pm
   
Any progress? I have tested it, it is working, so I suppose you got your answer. Am I right?
Junoli at 11-Apr-13 2:16am
   
Yes, it works
Zoltán Zörgő at 11-Apr-13 2:17am
   
Good. Than feel free to accept my answer. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Maciej Los 250
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100