rekna
rekna

Reputation: 5333

EF Code First, how to reflect on model

In EF code first, one specifies field properties and relationships using the fluent interface. This builds up a model. Is it possible to get a reference to this model, and reflect on it? I want to be able to retrieve for a given field, if it is required, what its datatype is, what length, etc...

Upvotes: 2

Views: 225

Answers (1)

Eranga
Eranga

Reputation: 32437

You need to access the MetadataWorkspace. The API is pretty cryptic. You may want to replace DataSpace.CSpace with DataSpace.SSpace to get the database metadata.

public class MyContext : DbContext
{

    public void Test()
    {            
        var objectContext = ((IObjectContextAdapter)this).ObjectContext;

        var mdw = objectContext.MetadataWorkspace;

        var items = mdw.GetItems<EntityType>(DataSpace.CSpace);
        foreach (var i in items)
        {
            foreach (var member in i.Members)
            {
                var prop = member as EdmProperty;
                if (prop != null)
                {

                }
            }
        }
 }

Upvotes: 2

Related Questions