SQLite with Entity Framework 6: relative path? - visual-studio

I'm trying to use SQLite with Entity Framework 6 in my WPF application. When I create Entity Data Model in the project, I connect to *.db file using absolute path, because relative path doesn't work for some reason. So my connection looks something like
<connectionStrings>
<add name="Model1ConnectionString" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite.EF6;provider connection string='data source="C:\Users\gtmaster\Documents\Visual Studio 2017\Projects\SQLiteTest\SQLiteTest\test.db"'" providerName="System.Data.EntityClient" />
</connectionStrings>
If some other developer clones reporsitory with this connection he gets unable to open database file error, because absolute path to test.db in the project is obviously different on his machine.
How can I write relative path in app.config so entity models could work without changing it everytime?

using relative path for the DB file
the source of the db file is in App.config
<add name="ProjectDBEntities" connectionString="metadata=res://*/ProjectDBModel.csdl|res://*/ProjectDBModel.ssdl|res://*/ProjectDBModel.msl;provider=System.Data.SQLite.EF6;provider connection string='data source="c:\Users\Heckl\Documents\Dropbox\Else\visual studio\SqLiteTry\ProjectDB.db"'" providerName="System.Data.EntityClient"/>
the data source must be absoulte
you can use the |DataDirectory| substitution string in App.config
<add name="ProjectDBEntities" connectionString="metadata=res://*/ProjectDBModel.csdl|res://*/ProjectDBModel.ssdl|res://*/ProjectDBModel.msl;provider=System.Data.SQLite.EF6;provider connection string='data source="|DataDirectory|ProjectDB.db"'" providerName="System.Data.EntityClient"/>
the substitution string has to be set
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
// ...
var db = new ProjectDBEntities()
problems:
exception at the first LINQ command
the change in App.config is not always detected by VS, you might have to rebuild solution manually
the code above set the location of the exe file in the solution\project\bin\debug folder
the db file is in the solution folder
check the value of db.Database.Connection.ConnectionString in a watch window during debug
you can copy the db file into the debug folder also

|DataDirectory| is good for production environment, but when you do update-database migrations apply to newly created database in EF packet directory, because EF cant get your project generated connection string and use self DataDirectory value.

Related

How to upload to SFTP user's Home directory using Spring Integration

I'm trying to upload a file via SFTP using Spring Integration (version 4.1.2).
Does anyone know how to configure the sftp:outbound-channel-adapter so that the file gets uploaded automatically to user's home directory without indicating the full directory path in the remote-directory's attribute (ex: remote-directory="/home/sftp_user")?
The solution is that the remote-directory must be set as an empty string.
Please note that the following won't work as it fails the XML model validation:
<sftp:outbound-channel-adapter
...
remote-directory=""
...
/>
I ended up reading the remote-directory from a configuration property which I set as an empty string.

accessing App.Config file after deploy in C#

Im using below code to update app.config's some values( I have config file path in the app.config file).When deploy its getting errors I think its becouse app.config file change in to an exe. how to change my code work as debug time as well as deploy time
var appPath = ConfigurationManager.AppSettings["configPath"].ToString();
string configFile = System.IO.Path.Combine(appPath, "App.config");
var configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();
Hi thanks every body for instance reply. I fix my own problem it was just a confusion. I was a java guy and I new to .net in .net App.config file compile and create .config in Debug folder file even though debug it access that .config file in Debug folder . So actually when if you change the value in App.config in programatically it doesn't change the App.config file. it change the .config which is in debug file.its like [project name].vshost.exe.config in debug folder.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["InvoiceInterval"].Value = InvoiceIntervalVal.ToString();
config.AppSettings.Settings["directPaymentInterval"].Value = directPaymentIntervalVal.ToString();
config.AppSettings.Settings["paymentStatusInterval"].Value = paymentStatusIntervalVal.ToString();
config.Save();
ConfigurationManager.RefreshSection("appSettings");
using above code you can able change App.config file's value in debug time also in run time. but those changes not seems in App.config file. but you can see changes in exe file which it is belongs to.In my case it was in src\Vetserve.Credicare\bin\Debug\Vetserve.Credicare.vshost.exe.config
Perhaps try and set your App.config's file 'Copy to Output Directory' property to 'Copy always'.
Reference: AppConfig file not found in bin directory
After compilation App.Config will be available as
YourConsoleApplication.exe.Config inside application bin.
You can do like:
var beforeInvoiceInterval = ConfigurationManager.AppSettings
["InvoiceInterval"].ToString();
ConfigurationManager.AppSettings["InvoiceInterval"] = "Your value";
var afterInvoiceInterval = ConfigurationManager.AppSettings
["InvoiceInterval"].ToString();
afterInvoiceInterval will contain the value you assigned but it'll not
modify YourConsoleApplication.exe.Config.

How to use a path relative to project root to H2 db-file configuration with Play Framework 2.4?

We're developing a Play 2.4 application (Java API).
For dev purposes, we'd like to use a persistent H2 database with DB file path relative to the project root directory.
In How to use a persistent H2 database in the Play Framework instead of in-memory there was solution for Play 2.0:
db.default.url="jdbc:h2:file:data/db"
However, with Play 2.4 this doesn't seem to work but I get error message with the following exception at the bottom:
Caused by: org.h2.jdbc.JdbcSQLException: A file path that is implicitly
relative to the current working directory is not allowed in the database
URL "jdbc:h2:file:data/db". Use an absolute path, ~/name, ./name, or the
baseDir setting instead. [90011-187]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
...
I could get connection to work with an absolute path and with a path relative to the home directory, like the following:
db.default.url="jdbc:h2:file:/Users/foo/data/db"
or
db.default.url="jdbc:h2:~/data/db"
However, is there some way to refer to the project root folder?
Ok, I did a little research and found this in the changelog (http://www.h2database.com/html/changelog.html):
Implicit relative paths are disabled (system property "h2.implicitRelativePath"), so that the database URL jdbc:h2:test now needs to be written as jdbc:h2:./test.
In H2 starting from version 1.4.177 Beta, implicit relative paths are not allowed anymore. Therefore, in your case the url should be written with a explicit relative path: db.default.url="jdbc:h2:./data/db".
A fixed or relative path can be used. When using the URL jdbc:h2:file:./data/sample
http://www.h2database.com/html/faq.html
now a relative path can be used.
for example,
jdbc:h2:file:./../../h2db;

Get MSDeploy to skip specific folders and file types in folders as CCNet task

I want MSDeploy to skip specific folders and file types within other folders when using sync. Currently I'm using CCNet to call MSDeploy with the sync verb to take websites from a build to a staging server. Because there are files on the destination that are created by the application / user uploaded files etc, I need to exclude specific folders from being deleted on the destination. Also there are manifest files created by the site that need to remain on the destination.
At the moment I've used -enableRule:DoNotDeleteRule but that leaves stale files on the destination.
<exec>
<executable>$(MsDeploy)</executable>
<baseDirectory>$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\</baseDirectory>
<buildArgs>-verb:sync
-source:iisApp="$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\"
-dest:iisApp="$(website)/$(websiteFolder)"
-enableRule:DoNotDeleteRule</buildArgs>
<buildTimeoutSeconds>600</buildTimeoutSeconds>
<successExitCodes>0,1,2</successExitCodes>
</exec>
I have tried to use the skip operation but run into problems. Initially I dropped the DoNotDeleteRule and replaced it with (multiple) skip
<exec>
<executable>$(MsDeploy)</executable
<baseDirectory>$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\</baseDirectory>
<buildArgs>-verb:sync
-source:iisApp="$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\"
-dest:iisApp="$(website)/$(websiteFolder)"
-skip:objectName=dirPath,absolutePath="assets"
-skip:objectName=dirPath,absolutePath="survey"
-skip:objectName=dirPath,absolutePath="completion/custom/complete*.aspx"
-skip:objectName=dirPath,absolutePath="completion/custom/surveylist*.manifest"
-skip:objectName=dirPath,absolutePath="content/scorecardsupport"
-skip:objectName=dirPath,absolutePath="Desktop/docs"
-skip:objectName=dirPath,absolutePath="_TempImageFiles"</buildArgs>
<buildTimeoutSeconds>600</buildTimeoutSeconds>
<successExitCodes>0,1,2</successExitCodes>
</exec>
But this results in the following:
Error: Source (iisApp) and
destination (contentPath) are not compatible for the given
operation.
Error count:
1.
So I changed from iisApp to contentPath and instead of dirPath,absolutePath just Directory like this:
<exec>
<executable>$(MsDeploy)</executable
<baseDirectory>$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\</baseDirectory>
<buildArgs>-verb:sync
-source:contentPath="$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\"
-dest:contentPath="$(website)/$(websiteFolder)"
-skip:Directory="assets"
-skip:Directory="survey"
-skip:Directory="content/scorecardsupport"
-skip:Directory="Desktop/docs"
-skip:Directory="_TempImageFiles"</buildArgs>
<buildTimeoutSeconds>600</buildTimeoutSeconds>
<successExitCodes>0,1,2</successExitCodes>
</exec>
and this gives me an error: Illegal characters in path:
< buildresults>
Info: Adding MSDeploy.contentPath (MSDeploy.contentPath).
Info: Adding contentPath (C:\WWWRoot\MySite
-skip:Directory=assets
-skip:Directory=survey
-skip:Directory=content/scorecardsupport
-skip:Directory=Desktop/docs
-skip:Directory=_TempImageFiles).
Info: Adding dirPath (C:\WWWRoot\MySite
-skip:Directory=assets
-skip:Directory=survey
-skip:Directory=content/scorecardsupport
-skip:Directory=Desktop/docs
-skip:Directory=_TempImageFiles).
< /buildresults>
< buildresults>
Error: Illegal characters in path.
Error count: 1.
< /buildresults>
So I need to know how to configure this task so the folders referenced do not have their contents deleted in a sync and that that *.manifest and *.aspx files in the completion/custom folders are also skipped.
The issue with this was... line breaks!
Where I'd split each -skip directive to a new line that was causing the illegal characters in path. Running all the skip directives inline has solved this:
<exec>
<executable>$(MsDeploy)</executable>
<baseDirectory>$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\</baseDirectory>
<buildArgs>-verb:sync
-source:contentPath="$(ProjectsDirectory)$(projectName)$(ProjectsWorkingDirectory)\Website\"
-dest:contentPath="C:\WWWRoot\$(websiteFolder)" -skip:Directory="assets" -skip:Directory="_TempImageFiles" -skip:objectName=dirPath,absolutePath="\\Desktop\\Docs"
</buildArgs>
<buildTimeoutSeconds>600</buildTimeoutSeconds>
<successExitCodes>0,1,2</successExitCodes>
</exec>
Take a look at this MSDN article entitled Web Deployment: Excluding Files and Folders via the Web Application’s Project File. Specifically the section "Excluding Specific Files / Folders". This will stop directories, files, and file/dir pattern matches from both being included as content in the deployment package as well as ignored on the destination when deployed.
However, I would take a step back and ask why do these files exist in your web project in the first place. The way I've handled user uploaded content on IIS is by adding a virtual directory to my web application. The contents of virtual directories (and the provisioning of the vdir itself) is ignored when doing a sync on a web deploy package. This also gives you the benefit of hosting the client content directory anywhere you like which has a whole score of advantages (i.e. bigger disk drive, prevention of denial of service by unscrupulous users who try to fill your hard disk with garbage data, etc.)

asp.net mvc 3 web.config connection string encryption

I have to deploy an asp.net mvc 3 website and its web.config contains database credentials.
After searching for a while, I found that one could place the connection string in a .cs file in App_Data folder, but if database password is changed, then the site needs to be recompiled.
Also I got to this link: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA , but the page says that the content is not retired.
Can someone please tell the updated practices to encrypt the connection string information in the web.config file.
Thanks.
Using an encrypt/ decrypt method on the particular web.config file
still seems to be the preferred practice
Classic implementation programmatically
I don't think the practices of encrypting web.config has updated with MVC, other than, obviously, you can't use an Event button to call the method as in the above example. You want to map the Encrypt/Decrypt methods to a controller action.
public ActionResult Encrypt()
{
ProtectSection("connectionStrings", "RSAProtectedConfigurationProvider");
return View();
}
private void ProtectSection(string sectionName,
string provider) {
Configuration config =
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section =
config.GetSection(sectionName);
if (section != null &&
!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}}
To Encrypt Connection string in Web.Config files, We can follow these steps.
Open C:\Windows\System32\CMD.exe As Administrator
In CMD type CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319 In CMD type
aspnet_regiis.exe -pef connectionStrings “Path of the Folder
containing the Web.Config file”
Ex: aspnet_regiis.exe -pef “connectionStrings” “D://PROJECTS/SAMPLE_PROJECT”
Set to identity impersonate false for project web.config
<system.web>
<identity impersonate="true" />
</system.web>
For Decryption, you can use the below command.
Open C:\Windows\System32\CMD.exe As Administrator
In CMD type CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319
In CMD type aspnet_regiis.exe -pdf “connectionStrings” “Path of the Folder containing the Web.Config file”
Ex: aspnet_regiis.exe -pdf “connectionStrings” “D://PROJECTS/SAMPLE_PROJECT”
Give thubms up to my article

Resources