1

I have a database, 90% created with EF 4.1 + Code First approach on a SQL Server 2012; the rest is generated by some SQL code (FUNCTIONS, COMPUTED COLUMNS, VIEWS, INDEXES, ETC.).

Now, I needed to use ObjectContext and at the same time optimize performance, so I created some SQL Views directly in the db, which basically do some calculations (count, mix, sum, etc.) on the already CF's generated tables.

I'd like to use the above "external" SQL views inside my solution, possibly pointing to the same connectionstring of my CF context and using with the same repository I created. I succeed to make an ADO.NET EDM of the Views (is this the right approach?), so now I have the Entity Model generated from db. For the reasons described above, in first instance I used the existing data connection and I choose to do not save the additional connection string inside my web.config.

Now I have the edmx containing myModel and myModel.Store of the "external" views. For example, here's an extract of mymodel.Designer.cs, which seems to be the standard one I've seen in other edmx of other projects:

 public partial class Entities : ObjectContext
{
    #region Constructors

    /// <summary>
    /// Initializes a new Entities object using the connection string found in the 'Entities' section of the application configuration file.
    /// </summary>
    public Entities() : base("name=Entities", "Entities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new Entities object.
    /// </summary>
    public Entities(string connectionString) : base(connectionString, "Entities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new Entities object.
    /// </summary>
    public Entities(EntityConnection connection) : base(connection, "Entities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    #endregion
    .............

I'd like to query the "external" entities. I did several tests, but I did not succeed. Could you tell me the right approach to the problem, please?

1) This is one of the tests I made. In this case I get an exception "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid":

 public class TManagerRepository : ITManagerRepository, IDisposable
   {
    private TManagerContext context; // the context pointing to CF entities
    private TManager.Models.Entities.SQL_Views.Entities entities; // the context pointing to the SQL views by the EDM

    public TManagerRepository(TManagerContext context)
    {
        this.context = context;
        this.entities = new TManager.Models.Entities.SQL_Views.Entities();

        var test = (from d in this.entities.myview
                        select d); 
    }

2) Then I tried to make a specific connection too, but I get an exception which says "Could not find the conceptual model to validate".

Thank you very much for your precious help!

Best Regards

Larry
  • 573
  • 5
  • 14
  • 31

1 Answers1

1

You cannot use same connection string for code first approach and for EDMX. Required connection strings have different format. So if you don't want to store connection string for EDMX context in your configuration file you must to built it manually.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you Ladislav. I am going to build the connection string manually. Anyway, trying to make a new connection, as I wrote for point 2, I get that exception... Moreover, I still have dubts this is a right approach the coexistence of CF with EDM ... what do you think? Do you think there are "better" solutions? I say that CF has taken me away most of the work, then I would like to keep it. THX – Larry Feb 23 '12 at 15:08