views:

176

answers:

2

I'm writing a little utility to load Entity Data Models from an assembly and query against them.

I've tried using Reflection to load a derived ObjectContext, but ObjectContext doesn't derive from MarshalByRefObject so I can't pass parameters to the constructor with Activator.CreateInstance() (according to the exception I get.)

Is it possible to load an Entity Model defined and stored in an external assembly using Reflection, "late bind" to the EDM, and perform queries against it if you provide a valid connection string?

A: 

The EDM Model that derives from the ObjectContext has the ConnectionString name which it passes to the base constructor from it's own default constructor. Like below.

    public const string ConnectionString = "name=My_Entities";
    public const string ContainerName = "My_Entities";

    #region Constructors

    public My_Entities()
        : base(ConnectionString, ContainerName)
    {
        Initialize();
    }

    #endregion

You can always change the Connection details via the ObjectContext.Connection.

willbt
This is a parameterless constructor, but it requires a config file entry with that name containing the connection string. I need to be able to pass in a string or a ready-to-go EntityConnection, which I can't do use CreateInstance.
Dave Swersky
+3  A: 

I believe ObjectContext.Metadataworkspace.LoadFromAssembly() is what your looking for:

http://msdn.microsoft.com/en-us/library/bb495513.aspx

jfar
This is exactly it! With a few other reflection tricks I'm able to connect to an assembly with an EDM, point to a database, and query without knowing anything about the entities in the EDM! Thanks!
Dave Swersky
Thanks, are you making a generic EntityFramework management studio or something?
jfar