locked
Entity Framework: Entity Model in a separated dll RRS feed

  • Question

  • User-894061110 posted

    I have a project where all Entity Models are in a separated dll,


    to give a corrected connection in Global.asax I have add this :

    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
    entityBuilder.Provider = "System.Data.SqlClient";
    entityBuilder.ProviderConnectionString = "server=myserver;database=mydb;Integrated Security=SSPI";
    entityBuilder.Metadata = @"res://*";
    OfficeBox.Settings.SystemEntities objContext = new OfficeBox.Settings.SystemEntities(entityBuilder.ToString());
                  
          
    model.RegisterContext(() => new OfficeBox.Settings.SystemEntities(entityBuilder.ToString()), new ContextConfiguration() { ScaffoldAllTables = true });


    and seem run ok !! (on default page the gridview is filled with all my tables)

    when click to one, this error occour :

    "The specified named connection is either not found in the configuration, not intended to be used with the EntityClient Provider, not valid."


    How resolve this error ?


    Thank You

    Monday, July 19, 2010 5:15 AM

Answers

  • User-330204900 posted

    There must be an issue with the class library, are you using VS2010 or VS2008?

    I can send my sample if you e-mail me using the Contact->Send sjnaughton an e-mail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 19, 2010 8:09 AM
  • User-330204900 posted

    Hi Marco, have a look at this article by Rick Anderson here the pertinent part is quoted below

    For Dynamic Data, simply pass the construction string to the MetaModel RegisterContext as follows.

    public static void RegisterRoutes(RouteCollection routes) 
    {
        MetaModel model = new MetaModel();
    
        model.RegisterContext(() => new NorthwindModel.NorthwindEntities(getConStrIntegrated()),
                new ContextConfiguration()
                {
                    ScaffoldAllTables = true
                });
    
        // Routes omitted for clarity
    }

    Unfortunately, the current version of Dynamic Data doesn't support this approach with EF (L2S does work). To get the page templates to use the connection string you must add the following line to the Page_Load method in the page templates.

    GridDataSource.ContextCreating += delegate(object ceSender, System.Web.UI.WebControls.EntityDataSourceContextCreatingEventArgs ceArgs)
      {
        ceArgs.Context = (System.Data.Objects.ObjectContext)table.CreateContext();
      };

    This is obviously something fixed in DD4 and .Net 4

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 6:01 AM

All replies

  • User-330204900 posted

    Hi , it seems to work for me have a look at my Global.asax.cs: 

    public class Global : System.Web.HttpApplication
    {
        private static MetaModel s_defaultModel = new MetaModel();
        public static MetaModel DefaultModel
        {
            get { return s_defaultModel; }
        }
    
        public static void RegisterRoutes(RouteCollection routes)
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SqlClient";
            entityBuilder.ProviderConnectionString = "server=aragorn;database=Northwind;Integrated Security=SSPI;MultipleActiveResultSets=True";
            entityBuilder.Metadata = @"res://*";
            var objContext = new NorthwindContext.NorthwindEntities(entityBuilder.ToString());
    
            DefaultModel.RegisterContext(() => new NorthwindContext.NorthwindEntities(entityBuilder.ToString()),
                        new ContextConfiguration()
                        {
                            ScaffoldAllTables = true
                        });
    
    
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });
        }
    
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

    Note that I tried it in an DD4 Web Application Project.

    Monday, July 19, 2010 6:12 AM
  • User-894061110 posted

    Sorry Steve,


    Give me same error..

    Monday, July 19, 2010 7:30 AM
  • User-330204900 posted

    There must be an issue with the class library, are you using VS2010 or VS2008?

    I can send my sample if you e-mail me using the Contact->Send sjnaughton an e-mail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 19, 2010 8:09 AM
  • User-330204900 posted

    Hi Marco, I now get the same error as you in VS2008 and I am looking into it , it looks to me like the default constructor is being called regardless of the one used in the Global.asax.cs file.

    Tuesday, July 20, 2010 5:52 AM
  • User-330204900 posted

    Hi Marco, have a look at this article by Rick Anderson here the pertinent part is quoted below

    For Dynamic Data, simply pass the construction string to the MetaModel RegisterContext as follows.

    public static void RegisterRoutes(RouteCollection routes) 
    {
        MetaModel model = new MetaModel();
    
        model.RegisterContext(() => new NorthwindModel.NorthwindEntities(getConStrIntegrated()),
                new ContextConfiguration()
                {
                    ScaffoldAllTables = true
                });
    
        // Routes omitted for clarity
    }

    Unfortunately, the current version of Dynamic Data doesn't support this approach with EF (L2S does work). To get the page templates to use the connection string you must add the following line to the Page_Load method in the page templates.

    GridDataSource.ContextCreating += delegate(object ceSender, System.Web.UI.WebControls.EntityDataSourceContextCreatingEventArgs ceArgs)
      {
        ceArgs.Context = (System.Data.Objects.ObjectContext)table.CreateContext();
      };

    This is obviously something fixed in DD4 and .Net 4

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 20, 2010 6:01 AM
  • User-894061110 posted

    Thank You Steve !


    It was a ConnectionString problem !!

    Tuesday, July 20, 2010 7:15 AM