views:

882

answers:

1

Hello,

I'm trying to filter entities based on their store types (either table or view).

I have 2 entities in my test project, one's source is a table and the other's source is a view.

<EntitySet Name="Test" EntityType="TestModel.Store.Test" store:Type="Tables" Schema="dbo" />
<EntitySet Name="TestView" EntityType="TestModel.Store.TestView" store:Type="Views" store:Schema="dbo" store:Name="TestView">

The code sample above is taken from model's edmx file's SSDL section.

I think the store:Type information in SSDL is what i need but i couldn't find a way to retrieve that value using entity-framework api.

Any help will be appreciated.

Regards,

Cankut

+1  A: 

Well you can query the metadata in the SSDL by looking in the SSpace or the StoreItemCollection.

i.e.

var sspaceEntitySets = context.MetadataWorkspace
                       .GetItems<EntityContainer>(DataSpace.SSpace)
                       .First().BaseEntitySets.OfType<EntitySet>();
var entitySet = sspaceEntitySets.First();
var tableType = entitySet
.MetadataProperties["http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator:Type"]
.Value.ToString();

Unfortunately this isn't going to help you tie your classes to whether they come from a table of a view. Because the Entities (i.e. the ones you code against in CSpace) rather than the ones that describe the shapes of the table (i.e. SSpace ones) are in CSpace and in order to know whether an Entity comes from a view or table, you would need to be able to get from the CSpace EntitySet to SSpace EntitySet via the Mapping.

Unfortunately the EF doesn't expose public CSSPace (i.e. there is no way to use the API to read the MSL fragment of the EDMX).

So in order to do this you would have to manually reason over the MSL element, probably using LINQ to XML or something.

Hope this helps

Alex

Alex James
var sspaceEntitySets = context.MetadataWorkspace .GetItems<EntityContainer>(DataSpace.SSpace) .First().BaseEntitySets.OfType<EntitySet>();this line throws InvalidOperation exception saying "The space 'SSpace' has no associated collection."
Cankut
Thanks Alex, i got the error above because SSDL is not loaded until it's needed. A small "ToTraceString()" solved the problem.http://thedatafarm.com/blog/data-access/quick-trick-for-forcing-metadataworkspace-itemcollections-to-load/
Cankut