First of all
I want to separate my web.config file and any sensitive data by separating into 2 different files.
Something like
web.config
sensitiveData.config
Moreover
I need to this to be configuration dependent. This can be implemented by xml-transform. But I don't know how to make web.config parametrised and configuration dependent.
How I tried
My idea is to add keys into appSettings and use in web.config, additionally here is a property appSettings.File where can be stored file with params and in xml-transformations I can simply change file to change configuration.
Well, I know about appSettings, I have added some
</appSettings>
<add key="MyConnstring" value="connstring" />
</appSettings>
But it is a mistake, because this can't be done, this is incorrect
<connectionStrings>
<add name="sqlwork" providerName="Oracle.DataAccess.Client" connectionString=MyConnstring />
</connectionStrings>
To summarise
Is there any ideas how it can be done to perform.
Sensitive data in a separate file
Configuration dependent sensitive data
its possible to move connection strings and other sensitive data in appsettings.
<configuration...
<appSettings configSource="config/appSettings.config"/>
<connectionStrings configSource="config/connections.config"/>
....
Here, appSettings.config in config folder contains sensitive app settings and connections.config contains the connection strings.
Reference: How can I simplify my Web.config file?
You can still read the settings and connection strings using ConfigurationManager.
No answer required - experience documented for others
I wasted significant time and hope to save others the trouble
TLDR;
If you are trying to use Microsoft Dynamics CRM with an ASP.NET 5 MVC 6 (dnx / vnext) application, don't overthink it - just use the existing web.config file!
Personally, I got so wrapped up in the new configuration paradigm that the now obvious approach of just using the good 'ol web.config file to store the required configuration settings did not occur to me.
I was trying to connect to Microsoft Dynamics CRM; everything compiled and executed until I hit code that tries to create a CRM context generated using CrmSvcUtil.exe.
var xrm = new XrmServiceContext("Xrm");
The runtime error I got was, "A configuration element with the name 'Xrm' under the 'contexts' collection does not exist."
This simply means that there is something wrong with the configuration file. Now, I had an existing console application with the proper (working) configuration settings and I was trying to simply port them over to this vNext solution:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
</configSections>
<connectionStrings>
<add name="Xrm" connectionString="Server=https://myserver.crm.dynamics.com; Domain=; Username=myUsername#domain.com;Password=myPassword"/>
</connectionStrings>
<microsoft.xrm.client>
<contexts default="Xrm">
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm"/>
</contexts>
</microsoft.xrm.client>
</configuration>
My mistake, however, was a misguided attempt to use the new configuration paradigm and import the "Microsoft.Framework.Configuration.Xml" NuGet package. Then, I added the configuration file containing the necessary settings during Startup(): Again, this isn't the right approach; use web.config instead:
var builder = new ConfigurationBuilder(env.WebRootPath)
.AddXmlFile("config.xml")
.AddEnvironmentVariables();
It took me writing out the speculation here that the CRM dll's have no clue about the new IConfigurationSource approach. I knew there had to be another way and then my Friday night tired brain finally clicked..."Hey, I see a web.config file...I wonder if that would work." Yes, it does.
I integrated active directory with sitecore and it works perfect, now i am trying to write patches for the config changes. Sections <membership defaultProvider="sitecore" hashAlgorithmType="SHA1"> and <roleManager defaultProvider="sitecore" enabled="true"> are changed for connection setting to AD. When i try to write config patch for this section, this section is not built at run time. But the domains section works, i mean the patch i created for this section works and writes to web.config at runtime. I observed a difference here Domains section is under <Sitecore>, <membership> and <roleManager > are in <system.web> section. Is this the reason that these are not included in web.config? can we write patches for those sections only under <sitecore>?
Any ideas are appreciated.
Thanks.
You can only patch elements within the /configuration/sitecore element.
Refer this post:
http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/All-About-Web-config-Include-Files-with-the-Sitecore-ASPNET-CMS.aspx
I think you have to use config transforms as mentioned by leandro.
I assumming you are using Visual Studio ¿? You must specify in what are you working.
If so, you need create a transform file for the build configuration that you need, for example, one config for every environment or publish type.
Try a look at this:
http://msdn.microsoft.com/en-us/library/vstudio/dd465318%28v=vs.100%29.aspx
I am learning ASP.NET MVC from tutorials of Microsoft :
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-https://stackoverflow.com/editing-helpdata-from-a-controller
At the link above mentioned, while adding a controller named "MoviesController" , i am getting this error
"Unable to retreive metadata for 'MvcMovie.Models.Movie'. Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used"
How can i fix that?
I had the same issue.
I switched providerName="System.Data.SqlServerCe.4.0" with providerName="System.Data.SqlClient", and it created the Controller and Views.
I found this: http://msdn.microsoft.com/en-US/library/ms171861.aspx
I followed the directions and added a reference to SQL Server Compact, but it still doesn't work.
I also tried commenting out the default SQL Server Express connection, but it still gave the same error when trying to add the controller.
I'm just going to use SQL Server Express. I will let you know if I have any issues.
I've found that using the following works:
(Assuming that you've got SQL Server Express or higher installed)
<add name="MovieDBContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=|DataDirectory|MovieDB.sdf"/>
I think the reason of this error is because the VS is having some difficult to reuse your existing DBContext, while scaffolding. The VS try to use a property in DBContext with similar name of the Domain (like trying the Domain name concatenating "s"). If it cant find the property and your context already has a DBSet with you Domain class, it generates that error.
My solution was to create a new DBContext named "DeleteContext". After creating with success the controller and views, I have replaced the "DeleteContext" in my Controller to my existing one. Finally I deleted the "DeleteContext" class.
It works really fine.
That solution was not working for me, it blows on return View(db.Movies.ToList());
Instead use this :
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movie.sdf" providerName="System.Data.SqlServerCe.4.0"/>
and this:
public class MovieDBContext : DbContext
{
public MovieDBContext() : base("Movie") { }
public DbSet<Movie> Movies { get; set; }
}
I ran into the same error on another Microsoft ASP.NET MVC tutorial while using Visual Studio 2012 (http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5).
I decided that for the sake of completing the tutorial, it was easier to just use SQL Express in place of SQL CE.
What I did to resolve the issue was delete MvcMusicStore.sdf from Server Explorer, then deleted the same database file from App_Data in Solution Explorer.
I updated the connection strings section in the Web.config to use a Sql Express database (.mdf) in place of Sql CE (.sdf). For this particular tutorial, MusicStoreEntities is the name of the class that extends DbContext:
<connectionStrings>
<add name="MusicStoreEntities" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
I built the solution, ran the site, and the database was regenerated for me. To add the database file back to the solution, click the "Show All Files" button in Solution Explorer, right-click the .mdf file in App_Data, and select "Include in Project."
Hi there's one solution that worked fine to me. In your Web.config, the tutorial told you to add the following line in the connectionStrings section:
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
Do not delete it or change it!
When you're adding a new controller you can make this to avoid the error that is presenting:
Comment the line above.
Add the following line:
<'add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlClient"/>
Save your solution
Add the controller
Before you run your app, uncomment the line you have commented (1), and comment the line you added (the one with the providerName="System.Data.SqlClient") (2).
This worked very fine to me, when adding the controllers.
Hope this could help you.
I just tryed a bit around and found the problem.
In the tutorial you are adding the following line in your web.config
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/>
It seems that there is a problem with it.. i just commented it out and used the SqlServer and it works for me. If you still want to use the SqlServerCe you need to take a look how to fix that problem.
If you're using VS 2012, you will need to also tell EF to use SQL Compact instead of localDb.
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
An easier way is to install the EF SQL Compact Nuget package.
For more details, check out this blog entry.
I ran into this same problem while working through the Contoso University asp.net MVC tutorial. It appears that the problem comes from mixing the SQL Server Compact connection strings with the Membership provider Sql Server connection.
I initially used hyperGeoMetric's fix, and that did work. Then I looked at the downloadable code's web.config and noticed some additional configuration.
If you add/replace the default parameter of entityFramework with this:
<parameters>
<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
add a system.data section like this:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
and modify the existing (default) DefaultConnection to look like this:
<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|aspnet-membership.sdf" />
After these modification, I was able to continue the tutorial using the Sql Server Compact edition.
Sam
I followed that tutorial too, and got the same issue. I deleted the connection string, then I was able to add the controller, and it created the other files. Then I added the same connection string again to the Web.config file, inserted it ecxactly to the same place where it was before. It solved my problem.
I'm learning this tutorial:http://mvcmusicstore.codeplex.com/. and got the same error. I found a item name "EntityFramework.SqlServerCompact" in the NuGet packages list, and install it, all things go fine! note, the packages dependent entityframework, you can view all the version here:https://www.nuget.org/packages/EntityFramework.SqlServerCompact. GOOD LUCK!
the path of data source is wrong , you can add a "\" before "Movie.sdf".
like this:
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|\Movie.sdf" providerName="System.Data.SqlServerCe.4.0"/>
I ran into the same problem in another solution. It appeared just after I introduced a parametrized call to the base constructor, like the one included in the default UsersContext class.
This fails
public class MovieDBContext : DbContext
{
public MovieDBContext() : base("DefaultConnection")
{
}
...
}
This works
public class MovieDBContext : DbContext
{
// No constructor here
...
}
It seems that naming the connection string in the constructor creates the error. I only have one connection string in my web.config, so DefaultConnection is still used, although I don't name it explicitly.
I finally have TeamCity setup to build on Source Control changes, and a separate MSBuild task setup to package and deploy to IIS on the staging server using the 'package' target and the generated 'deploy.cmd' script. Everything is perfect in terms of build events, file inclusion/exclusion, etc...
However, I've come across a problem with how the package is being deployed. Whenever I deploy the package to the server, the IIS settings get blown away. For example, I can set cache expiration headers or turn on static compression, and after I deploy my package they will revert to the default values of the server.
Does anyone know how I can get around this? Is there a parameter I can pass or rule I can ignore?
I think what you are running into is different from what you might think.
In IIS 7 when you set the values for properties like you are describing the configuration for that is stored in the web.config for the application, and not applicationHost.config. For example I just created a site and modified those settings, then inside my web.config file the following fragment was dropped in.
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<urlCompression doStaticCompression="false" />
<caching>
<profiles>
<add extension=".aspx" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
</profiles>
</caching>
</system.webServer>
So what is happening when you perform a sync the web.config in your package is overwriting the web.config which has the modified properties in it. What you need to do to configure your application in IIS 7 as you want it, then grab the node from place it in your web.config file. Alternatively if you want you can place it in either web.debug.config or web.release.config if you want to only have that in your web.config when publishing.