2

我的 Global.asax 包含以下代码,

public class Global : System.Web.HttpApplication
    {
        private MetaModel _s_Model = new AdvancedMetaModel();
        public MetaModel s_Model
        {
            get
            {
                return _s_Model;
            }
        }

        private MetaModel _a_Model = new AdvancedMetaModel();
        public MetaModel a_Model
        {
            get
            {
                return _a_Model;
            }
        }

        public void RegisterRoutes(RouteCollection routes)
        {
            Dictionary<Helper.ModelName, MetaModel> registeredRoutes = new Dictionary<Helper.ModelName, MetaModel>();

            if (SQLAppModel.ModelQuery.GetUserType() == Utility.Helper.UserType.ApplicationAdmin
                || SQLAppModel.ModelQuery.GetUserType() == Utility.Helper.UserType.SystemAdmin)
            {
                _a_Model.RegisterContext(typeof(SQLAppModel.aEntities), new ContextConfiguration() { ScaffoldAllTables = true });

                /** Full Permission **/
                routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
                {
                    Action = PageAction.List,
                    ViewName = "ListDetails",
                    Model = a_Model
                });

                routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
                {
                    Action = PageAction.Details,
                    ViewName = "ListDetails",
                    Model = a_Model
                });

                registeredRoutes.Add(Helper.ModelName.Administration, a_Model);
            }

            string supportedEnvironments = System.Configuration.ConfigurationManager.AppSettings[Helper.SupportedEnvironmentsAppSettingsKey].ToString();

            foreach (string supportedEnvironment in supportedEnvironments.Split(','))
            {

                foreach (var supportedSystem in SQLAppModel.ModelQuery.GetSupportedSystems(supportedEnvironment, true))
                {
                    if (supportedEnvironment.ToUpper() == "ORACLE")
                    {
                        if (supportedSystem.Name.ToUpper() == "ADS")
                        {
                            _s_model.RegisterContext(typeof(OracleAppModel.sEntities), new ContextConfiguration()
                            {
                                ScaffoldAllTables = true
                            });

                            routes.Add(new DynamicDataRoute("{table}/ReadOnlyListDetails.aspx")
                            {
                                Action = PageAction.List,
                                ViewName = "ReadOnlyListDetails",
                                Model = s_model
                            });

                            routes.Add(new DynamicDataRoute("{table}/ReadOnlyListDetails.aspx")
                            {
                                Action = PageAction.Details,
                                ViewName = "ReadOnlyListDetails",
                                Model = s_model
                            });
                            registeredRoutes.Add(Helper.ModelName.ADS, s_model);
                        }
                    }
                }

            HttpContext.Current.Session[Helper.RegisteredRouteListSessionKey] = registeredRoutes;
        }

        void Application_Start(object sender, EventArgs e)
        {

        }

        void Session_Start(object sender, EventArgs e)
        {
            SQLAppModel.ModelQuery.GetApplicationUser();
            RegisterRoutes(RouteTable.Routes);
        }
    }

当我第一次使用 ASP.NET 开发 Web 服务器调试我的应用程序时,该应用程序运行良好并给出了预期的结果。

但是当我停止调试并重新启动时,它给出了以下异常,

项目已添加。字典中的键:'APP.SQLAppModel.sEntities' 正在添加的键:'APP.SQLAppModel.sEntities'

抛出此异常的行是 _a_Model.RegisterContext(typeof(SQLAppModel.aEntities), new ContextConfiguration() { ScaffoldAllTables = true });

完整的堆栈跟踪:

[ArgumentException:项目已被添加。字典中的键:'APP.SQLAppModel.sEntities' 正在添加的键:'APP.SQLAppModel.sEntities'] System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) +9352427 System.Collections.Hashtable.Add(对象键,对象值)+11 System.Web.DynamicData.MetaModelManager.AddModel(类型 contextType,MetaModel 模型)+96 System.Web.DynamicData.MetaModel.RegisterContext(DataModelProvider dataModelProvider,ContextConfiguration 配置)+727 System.Web.DynamicData。 MetaModel.RegisterContext(Func`1 contextFactory, ContextConfiguration configuration) +390 System.Web.DynamicData.MetaModel.RegisterContext(Type contextType, ContextConfiguration configuration) +88 SAMI.Global.RegisterRoutes(RouteCollection routes) in C:\Anand\SAMI\SAMI \SAMI\Global.asax.cs:

请让我知道如何解决这个问题。我很难确定问题所在。

4

1 回答 1

0

问题是您从 Session_Start 调用 RegisterRoutes,因此可以多次调用它。您需要改为从 Application_Start 调用它。

于 2011-10-21T16:44:13.640 回答