Elmah code-first SQL Server logging - asp.net-mvc

I installed Elmah via nuget. It usually works will if I use an .edmx. However I am using a code-first DbContext. Elmah seems to be sending the emails but it doesn't log to the database.
Any idea what could be going wrong?

It will work just fine with code first. You do not need the ELMAH_ERROR table as a part of your context. As long as you ran the elmah SQL in your database and setup your config correctly it WILL log to the database. Please share your config code, it should look something like this (a small portion of the config):
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" applicationName="YOUR_APPLICATION" />
<security allowRemoteAccess="yes" />
</elmah>
<connectionStrings>
<add name="elmah-sqlserver" connectionString="Data Source=YOUR_SERVER;User ID=YOUR_USER_ID;Password=YOUR_PASSWORD;Initial Catalog=YOUR_CATALOG;" providerName="System.Data.SqlClient" />
</connectionStrings>

Related

Umbraco / Azure Blob Storage - Able to upload images & view in Umbraco, but getting 404 Errors on front-end

I recently began integrating UmbracoFileSystemProviders.Azure from https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure and am able to do the following on my local machine & a dev environment...
I can log into Umbraco (v7.3.1/a1.0.5780.28249), upload an image &
have it appear in the appropriate container on our Azure blob
storage
I can view images at their corresponding azure address (i.e. "https://azureaccount.blob.core.windows.net/media-stage/9999/file-name.png")
I can view images within Umbraco's backend (i.e. "http://localhost:99999/umbraco/backoffice/UmbracoApi/Images/GetBigThumbnail?originalImagePath=%2Fmedia%2F9999%2Ffile-name.png")
However, I get a 404 error when I try to view images on the front-end (i.e. "http://www.sitedomain.com/media/9999/file-name.png"). I think this is because the site is still trying to find the front-end image within the site rather than in Azure (based on the fact that creating/removing a matching directory & image at the appropriate relative location causes the image to load or yield a 404). This issue is consistent across multiple browsers & image uploads (whether I upload through Umbraco or through Microsoft Azure Storage Explorer). Does anyone have any ideas how I might be able to resolve it?
The following are all of the files/code snippets I'm using that I think might be relevant to the issue (with client-specific data swapped out in favor of generic data for security)...
Relevant section of Config/FileSystemProviders.config:
<Provider alias="media" type="Our.Umbraco.FileSystemProviders.Azure.AzureBlobFileSystem, Our.Umbraco.FileSystemProviders.Azure">
<Parameters>
<add key="alias" value="media"/>
</Parameters>
</Provider>
Relevant sections of Web.config:
<appSettings>
<!--other data here-->
<add key="AzureBlobFileSystem.ConnectionString:media" value="DefaultEndpointsProtocol=https;AccountName=azureaccount;AccountKey=aBcDeFgHiJkLmNoPqRsTuVwXyZ==;EndpointSuffix=core.windows.net" />
<add key="AzureBlobFileSystem.ContainerName:media" value="media-stage" />
<add key="AzureBlobFileSystem.RootUrl:media" value="https://azureaccount.blob.core.windows.net/" />
<add key="AzureBlobFileSystem.MaxDays:media" value="365" />
<add key="AzureBlobFileSystem.UseDefaultRoute:media" value="true" />
<add key="AzureBlobFileSystem.UsePrivateContainer:media" value="false" />
<!--other data here-->
</appSettings>
<location path="Media">
<system.webServer>
<handlers>
<remove name="StaticFileHandler" />
<add name="StaticFileHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
</handlers>
</system.webServer>
</location>
A few additional notes for the troubleshooting process...
I've seen a few similar issues posted on GitHub (& closed out), but I believe my situation to be unique because...
I am not seeing configuration errors when I view the project or
umbraco backend (as with
https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure/issues/103)
My Umbraco is version 7.3.1 (rather than 7.5+, like https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure/issues/57)
Commenting out the image processor in the web config does not solve the issue (as with
https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure/issues/27)
I've also tried forgoing the web.config approach & using the following within Config/FileSystemProviders.config (again, client-specific data has been replaced with generalized stuff), but still had the same problem...
<Provider alias="media" type="Our.Umbraco.FileSystemProviders.Azure.AzureBlobFileSystem, Our.Umbraco.FileSystemProviders.Azure">
<Parameters>
<add key="containerName" value="media-stage" />
<add key="rootUrl" value="https://azureaccount.blob.core.windows.net/" />
<add key="connectionString" value="DefaultEndpointsProtocol=https;AccountName=azureaccount;AccountKey=aBcDeFgHiJkLmNoPqRsTuVwXyZ==;EndpointSuffix=core.windows.net"/>
<add key="maxDays" value="365" />
<add key="useDefaultRoute" value="true" />
<add key="usePrivateContainer" value="false" />
</Parameters>
Thanks in advance for any assistance!
I found that I needed to disable the Virtual Path Provider otherwise the URLs generated were always relative (of the form /media/nnnnn/filename.jpg). Hence I added this to web.config AppSettings
<add key="AzureBlobFileSystem.DisableVirtualPathProvider" value="true" />
see https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure.
This was with Umbraco version 7.7.7.

MVC Controllers - Connection String / associations - No Connection String could be found in the application config file

Please excuse me is this is a noob question. I have a MVC project with a number of EF Database first models. When I created them some were created with new connection strings.
I have tried to clean up the project web.config file and commented out the duplicated connection strings,
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=........... />
<add name="DB_A09819_ProductsEntities" connectionString="metadata=........... />
<add name="DB_A09819_PowerDB001Entities" connectionString="metadata=........... />
<!--
<add name="DB_A09819_ProductsEntities_IntranetDownload" connectionString="metadata........... />
<add name="DB_A09819_ProductsPricing" connectionString="metadata=........... />
<add name="DB_A09819_ProductsProjectProposal" connectionString="metadata=.......... />
<add name="DB_A09819_ProductsProposalSystem" connectionString="metadata=........... />
-->
</connectionStrings>
I had thought that MVC would have picked up on the changes and raised errors, however with these changes the project builds and runs on development server.
When I try to add a controller I now get an error that the
---------------------------
Microsoft Visual Studio
---------------------------
Error
There was an error running the selected code generator:
'Unable to retrieve metadata for 'web...............t'.
No connection string named 'DB_A09819_..........'
could be found in the application config file.'
---------------------------
OK
---------------------------
I would like to change the associations and fix the connection string to have a single connection to each of the databases.
There are a number of posts where the solution is to add a connection string, I am trying to clean up my code and remove duplicate connection strings.
How do I change the association to the connection string in the Model and the controllers to ensure that it will run when deployed ?

How do I setup ASP.NET Identity to use my own connection string?

I have an MVC5 app that's using EF. I would like to add ASP.NET Identity and I've noticed that the connection string for ASP.NET identity is using "DefaultConnection". What Do I need to do so that ASP.NET Identity tables are created in my already existing db (source=DILS-S1301;initial catalog=MVC5;) as specified in MVC5Entities and NOT DefaultConnection => (LocalDb)\v11.0??
thanks
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MySuperAwesomeMVCApp-20131105011429.mdf;Initial Catalog=aspnet-MySuperAwesomeMVCApp-20131105011429;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MVC5Entities" connectionString="metadata=res://*/Models.Mvc5Model.csdl|res://*/Models.Mvc5Model.ssdl|res://*/Models.Mvc5Model.msl;provider=System.Data.SqlClient;provider connection string="data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I tried modifying "DefaultConnection" like so:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=DILS-S1301;AttachDbFilename=|DataDirectory|\MVC5.mdf;Initial Catalog=MVC5;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MVC5Entities" connectionString="metadata=res://*/Models.Mvc5Model.csdl|res://*/Models.Mvc5Model.ssdl|res://*/Models.Mvc5Model.msl;provider=System.Data.SqlClient;provider connection string="data source=DILS-S1301;initial catalog=MVC5;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
but now i get an error:
Database 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\MVC5.mdf' already exists. Choose a different database name.
Cannot attach the file 'C:\Users\blah\Documents\Visual Studio 2013\Projects\MySuperAwesomeMVCApp\MySuperAwesomeMVCApp\App_Data\MVC5.mdf' as database 'MVC5'.
The actual question shall be "How do I setup ASP.NET Identity to use my own connection string?"
If above is the correct summary of your question, below is the answer.
ASP.NET Identity uses EntityFramework for database related tasks. So you can use following option as suitable.
Option 1: EntityFramework's default connection string can be setup using following code snippet in web.config
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Option 2: Programatically, you can also pass ConnectionString name to the DbContext's constructor. like new ApplicationDbContext(MyConnectionString)
Actually all you have to do is change the DefaultConnection connectionString to be your desired connection string. If you used the normal settings for a new MVC 5 project, and the identity tables do not exist in the new DB they will be created there automatically.
You do not have to edit the portion of the connection string and you do not have to edit the DbContext constructor unless you want to change the conntectionString name and are not OK with replacing the default connection string.
If you are OK with replacing the default connection string, you should be able to just replace it... this worked for me.
I found this article helpful as well:
http://www.typecastexception.com/post/2013/10/27/Configuring-Db-Connection-and-Code-First-Migration-for-Identity-Accounts-in-ASPNET-MVC-5-and-Visual-Studio-2013.aspx

Switching from SQL Server Express to SDF database

I've created an mvc project with following connection string (which is a default):
<add name="TestDb"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I would like to switch from SQL Server Express to one of the file databases, so that I can store my db in same repository. How can I do this (steps/connection string) ?
I'm using MVC code first approach. Thank you.
Install the the EntityFramework.SQLServerCompact NuGet package.
Change your connection string to this:
<add name="TestDb"
providerName="System.Data.SqlServerCe.4.0"
connectionString=
"Data Source=|DataDirectory|\aspnetdb.sdf" />
With Code First, you typically use the context class name as your connection string name.

SimpleMembership API connectionStringName error

I am trying to use SimpleMembership in my MVC 3 application. However I get the following error:
System.ArgumentException was unhandled
by user code. Unable to find the
requested .Net Framework Data
Provider. It may not be installed.
I'm using EF and the framework is obviously there since my app works perfect without the SimpleMembership API
Here is the Set Up in the Web config file:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="SeniorProjectModelContainer" connectionString="metadata=res://*/Models.SeniorProjectModel.csdl|res://*/Models.SeniorProjectModel.ssdl|res://*/Models.SeniorProjectModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=GOGOTOPPY;Initial Catalog=SPMT;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
And heres how it looks in the in the Membership file in the App_Start folder:
WebSecurity.InitializeDatabaseConnection(connectionStringName: "SeniorProjectModelContainer", userTableName: "Users", userIdColumn: "UserId", userNameColumn: "UserName", autoCreateTables: true);
Any help would be greatly appreciated!
Update:
The quick work around was to take the metadata part out of the connection string and change the provider name to the SqlClient so it looks similar to this:
<connectionStrings>
<add name="Membership" connectionString="Data Source=serverName;Database=datebaseName;User ID=idName;Password=password;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Is there any downside of the connection string looking like this?
Not sure how you installed simplemembership but try doing it the nuget way for example,
http://www.nuget.org/List/Packages/SimpleMembership.Mvc3

Resources