locked
Register models (L2S) dynamic at runtime RRS feed

  • Question

  • User277375135 posted

    Is it possible to register one model with different connectionstrings two times or more?

    I tried with code bellow and it doesn't work. I'm getting this error: Item has already been added. Key in dictionary: 'Models.GrezniceDataContext' at line model.RegisterContext(() => …

    public static Dictionary<string, MetaModel> RegisteredModels { get; set; }
    public static void RegisterRoutes(RouteCollection routes)
    {
    	RegisteredModels = new Dictionary<string, MetaModel>();
    	var settings = ModuleSettings.GetAllSettings();
    	int i = 0;
    	foreach (var mSetting in settings.Values)
    	{
    		try
    		{
    			MetaModel model = new NotAClue.Web.DynamicData.CustomMetaModel(DDModels.GetVisibleColumns);
    			string mConnStr = mSetting.CONNECTION_STRING;
    			string mModel = mSetting.LinqDataModel;
    			System.Data.Linq.DataContext dc = DDModels.GetModel(mConnStr, mModel);
    			if (dc == null) 
    				continue;
    			//model.RegisterContext(() => { return DDModels.GetModel(mConnStr, mModel); }, new ContextConfiguration() { ScaffoldAllTables = true });
    			model.RegisterContext(() => DDModels.GetModel(mConnStr, mModel), new ContextConfiguration() { ScaffoldAllTables = true });
    
    			routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    			{
    				Constraints = new RouteValueDictionary(new { action = "Details|Edit|Insert|PopupInsert|PopupSelect|PopupSelectParcela" }),
    				Model = model
    			});
    
    			routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    			{
    				Constraints = new RouteValueDictionary(new { action = "List" }),
    				ViewName = "MyList",
    				Model = model,
    			});
    			string mKey = mSetting.KEY;
    			RegisteredModels.Add(mKey, model);
    			i++;
    		}
    		catch (Exception ex)
    		{
    		}
    	}
    }
    
    public static System.Data.Linq.DataContext GetModel(string connectionString, string modelName)
    {        
    	
    	switch (modelName.ToLower())
    	{
    		case "greznice":
    			return new Models.GrezniceDataContext(connectionString);
    		case "ekoloskiotoki":
    			return new Models.EkoloskiOtokiDataContext(connectionString);
    		case "javnarazsvetljava":
    			return new Models.JavnaRazsvetljavaDataContext(connectionString);
    		case "javneinzelenepovrsine":
    			return new Models.JavneInZelenePovrsineDataContext(connectionString);
    		case "oglasnetableinpanoji":
    			return new Models.OglasneTableInPanojiDataContext(connectionString);
    		case "registernepremicnin":
    			return new Models.RegisterNepremicninDataContext(connectionString);
    		case "soglasja":
    			return new Models.SoglasjaDataContext(connectionString);
    		case "solskepoti":
    			return new Models.SolskePotiDataContext(connectionString);
    	}
    	return null;
    }
    Wednesday, November 23, 2011 3:49 AM

All replies

  • User277375135 posted

    Or should I register every model only once with some default connection string and then change it at runtime?

    How could I change the connection string for Linq to sql model at runtime?

    Thursday, November 24, 2011 12:58 AM
  • User3866881 posted

    How could I change the connection string for Linq to sql model at runtime?

    As far as I know, you can create another instance of your DataContext in the "using" block:

    using (MyDataClassesDataContext db = new MyDataClassesDataContext(dynamicConnString))
    {
        ………………
    }

    Thursday, November 24, 2011 8:35 PM
  • User-751080518 posted

    I know, that I can specify connectionstring as parameter on datacontext decleration, but my problem is more DynamiData specific.

    In my case I have same tables in different databases, so I would like to use one model, but change the connection string, based on some querystring value, which tells me which connection string should I use. I tried to redister same model twice, with different connection string, but it throws error on line registercontext : duplicate key ...

    So probably I should change connectionstring on every pagetemplate of dynamicdata if I am right?

    How and where could I do that?

    Monday, November 28, 2011 4:36 AM
  • User3866881 posted

     I tried to redister same model twice, with different connection string, but it throws error on line registercontext : duplicate key ...

    Hello:)

    Does it have anything to do with the connection string? Duplicated Keys ususally means when you do inserting or updating, you've duplicated to insert a value that is duplicated, perhaps it's either primary key or unique key.

    Show us your full codes, plz

    Best reguards!

    Monday, November 28, 2011 8:15 PM
  • User-751080518 posted

    Actually this problem is more Dynamic Data related and it has nothing to do with primary keys and unique keys.

    I have many LinqToSql models (.dbml) and each of theme is ment to be used for more then one database.

    At application start Dynamic Data is registering these models. I tried to register these models for every database, but looks like

    the key for register context is model name, and even if I have different connection string it throws error duplicate key.

    So I will have to change the connection string on every PageTemplate.

    Does anyone know how and where can I do that? Probably I have to overide datacontext_oncreate event?

    Tuesday, November 29, 2011 3:48 AM