none
How do I access the elmah database from code? RRS feed

  • Question

  • VS2013 update 4, MVC5, elmah.mvc (same as elmah), VB/C#

    The following code is part of the standard MVC template to get started and it is part of setting up our database connection from within code to access an SQL database:

    Public Class ApplicationDbContext
      Inherits IdentityDbContext(Of ApplicationUser)
      Public Sub New()
        MyBase.New("DefaultConnection", throwIfV1Schema:=False)
        Me.Configuration.LazyLoadingEnabled = True
      End Sub
    
      Public Shared Function Create() As ApplicationDbContext
        Return New ApplicationDbContext()
      End Function

    We also need the following or something similar in web.config:

    <configuration>
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=mypc\SQLEXPRESS;Initial Catalog=mydb.DefaultContext;Integrated Security=True" providerName="System.Data.SqlClient" />
        <add name="ElmahConnection" connectionString="Data Source=mypc\SQLEXPRESS;Initial Catalog=mydb.elmah;Integrated Security=True" providerName="System.Data.SqlClient" />
      </connectionStrings>

    I added the elmah connection string as it is relevant to my question.

    Then in each location where needed, the following definition is added and we can access the database tables using db.< tablename>...:

    Private db As New ApplicationDbContext

    I want to write some methods to work with and on the elmah data table but it is a different context than ApplicationContext.  I don't quite understand what I would write to setup a connection to the elmah table since I didn't really set that up, it came in the dll for elmah.

    Wednesday, September 2, 2015 9:49 PM

Answers

All replies

  • Hi Alan,

    You are asking questions about elman.mvc, I think it's more related to asp.net and nuget package. I suggest you post thread on asp.net forum for effective response.

    http://forums.asp.net/25.aspx/1?Security

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, September 4, 2015 2:17 AM
    Moderator
  • Hi Herro,

    I will post there as you recommend, but I thought this would be a good forum because I'm essentially asking how do I set up a context for an existing database.  That's all.

    I guess in responding to you it occurred to me that I'm effectively wanting to use EF in Database First mode, yes?

    I've done so much Code First I didn't really consider that my issue may be resolved by simply employing Database First.  I'll study tomorrow.

    Best Regards,

    Alan


    Friday, September 4, 2015 2:53 AM
  • Hi Herro,

    I will post there as you recommend, but I thought this would be a good forum because I'm essentially asking how do I set up a context for an existing database.  That's all.

    I guess in responding to you it occurred to me that I'm effectively wanting to use EF in Database First mode, yes?

    I've done so much Code First I didn't really consider that my issue may be resolved by simply employing Database First.  I'll study tomorrow.

    Best Regards,

    Alan


    Yes, EF questions here. From your description, if there is available database, db first is more suitable than code first. Here is more info about the differences between db first and code first. Enjoy it.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 7, 2015 2:37 AM
    Moderator
  • I was able to accomplish my objective using Code First From Database which is pretty cool.

    The link to my solution is on Stack Overflow:

    http://stackoverflow.com/questions/32277942/how-do-i-access-the-elmah-database-from-code/32468319#32468319

    • Marked as answer by Alan Wheeler Tuesday, September 8, 2015 10:39 PM
    Tuesday, September 8, 2015 10:38 PM