How do I access the elmah database from code? - database

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.

While keeping error records makes sense, during development I wanted to clear the Elmah log. While probably trivial for most, it was a good exercise to figure it out. I was so used to code first, I forgot EF also does database first.
In VS2013 update 4 the following Wizard made this task pretty easy.
1) Project > Add New Item > Data > ADO.NET Entity Data Model
2) Give the model a name (With great creativity I used 'elmah') > Click Add
3) This is where it gets pretty cool. I'm not sure when it was added but the EF framework now includes a new choice called Code First From Database. Click on that.
4) Select the elmah connection string
Note: There is an option to save connection settings in web.config. That simply creates a duplicate connection string. Leave it checked or don't, but using the existing string is fine. I didn't select the option and web.config was left untouched. However, not using that option means the connection string name will have to be updated in the context definition > Click Next
5) Click on Tables to load the Elmah table, leave the defaults as is > Click Finish
The wizard creates 2 files and places them in the project. One is named by the data table name which is ELMAH_Error; this is the model. The other is a parital class that sets up the context. If the save connection option was selected then web.config will be modified to add the new connection string.
Note: I combined the 2 Classes into a single file for convenience (may require adding some Using or Imports) and the Public Sub New() is where the connection string name must be updated if the existing connection string is used as shown below.
Partial Public Class elmah
Inherits DbContext
Public Sub New()
MyBase.New("name=ElmahConnection")
End Sub
Public Overridable Property ELMAH_Error As DbSet(Of ELMAH_Error)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
End Sub
End Class
6) With all that automatically done (except for the connection string name if required), all that is required is to write the desired code. I wanted to empty the elmah log from time to time and used the following:
Public Class ErrorController
Inherits Controller
Private db1 As New elmah
Function DeleteElmahRecords() As ActionResult
Dim elmahTable = db1.ELMAH_Error.ToList()
db1.ELMAH_Error.RemoveRange(elmahTable)
db1.SaveChanges()
Return RedirectToAction("ErrorTesting")
End Function
…
End Class
Obviously I have a method called ErrorTesting that the function returns to and I access this method from a link on that same page.
Hope this is useful for someone. It's kind of the only way I can give back considering the awesome guru help I often get from this site.

Related

How to add-migrations for auto-generated databases?

I have an application which contains to context classes. One is master context class and second is sub context class. When I run the application for the first time the master database gets generated. And whenever I create a new user, a database related to that user gets generated using sub context class. For example, if I create 10 users then 10 databases will get generated using sub context class like sub_db_userId.
The problem is enabling migrations in this complex structure. I know some people would say it's not appropriate to create new database use foreign key relation but I have to work on requirements.
I found this thread very helpful in finding how to enable-migrations for separate context classes but in my scenario, it does not apply changes to existing databases because of database name is associated with user ids. Rather applying changes to existing child databases it creates new database without user id like this sub_db_. How can I solve this issue?
The way I am creating the new database for every user is given below.
My context classes:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false) {}
}
public class SubDbContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<City> Cities { get; set; }
public SubDbContext() : base("SubDatabaseConnection")
{
}
public SubDbContext(string connectionString) : base(connectionString)
{
Database.SetInitializer<SubDbContext>(new
CreateDatabaseIfNotExists<SubDbContext>());
}
}
Connection strings:
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=master_db;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="SubDatabaseConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog={0};Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I am using {0} for formatting purposes. How can I enable migrations and apply changes to existing databases?
Modifications: I am linking tables to database in Register action like this:
SubDbContext newContext = new SubDbContext(string.Format(userDatabase, "sub_db_" + userId));
newContext.Countries.FirstOrDefault();
newContext.Cities.FirstOrDefault();
Your DbContext for all your sub-databases is SubDbContext. So you must enable migration based on one of them. then put this code as Configuration class:
internal sealed class Configuration : DbMigrationsConfiguration<SubDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
}
and put this code into the SubDbContext constructor:
Database.SetInitializer<SubDbContext>(new MigrateDatabaseToLatestVersion<SubDbContext, Configuration>());
it means, every database when found a new manual added Migration,
then try to migrate it.
and also you can use this approache too:
write this code in the Application_Start:
var context = new SubDbContext("your generated connection string");
var initializeMigrations = new MigrateDatabaseToLatestVersion<SubDbContext, Configuration>();
initializeMigrations.InitializeDatabase(context);
I hope you find it helpful.

Change connection EF at runtime in WPF app

I have a WPF app that I'd like to change the connection string programmatically when the app loads. I use the Database-First approach for EF.
I spent a lot of time implementing various solutions found online including stack overflow and can't seem to get it to work.
The most common way seems to be to modify the Entity partial class. When I do this I get the following error at runtime:
Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
I got the same error implementing various other ways as well. If someone could please help me implement a way to change the connection string at runtime I'd greatly appreciate it.
My current implementation is taken from this example solution:
Changing Databases at Run-time using Entity Framework
Implementation:
App.config containts the connection string and name
Partial class added with same name as Entity class:
Imports System.Data.Entity
Imports System.Data.EntityClient
Imports System.Data.SqlClient
Partial Public Class MyEntityName
Inherits DbContext
Public Sub New(ByVal connString As String)
MyBase.New(connString)
End Sub
End Class
In my Application.xaml code file I set a global string variable by calling a method that builds the EntityConnectionStringBuilder. This global string variable is then passed into an entity constructor.
Imports System.Reflection
Imports DevExpress.Xpf.Core
Imports System.Data.EntityClient
Class Application
Public Sub New()
entityConnStr = BuildConnectionString("[MyDataSource]", "[MyDatabase]")
End Sub
Private Function BuildConnectionString(ByVal DataSource As String, ByVal Database As String) As String
' Build the connection string from the provided datasource and database
Dim connString As String = "data source=" & DataSource & ";initial catalog=" & Database & ";persist security info=True;user id=[user];password=[password];trustservercertificate=True;MultipleActiveResultSets=True;App=EntityFramework""
' Build the MetaData... feel free to copy/paste it from the connection string in the config file.
Dim esb As New EntityConnectionStringBuilder()
esb.Metadata = "res://*/DB.[MyEntityName].csdl|res://*/DB.[MyEntityName].ssdl|res://*/DB.[MyEntityName].msl"
esb.Provider = "System.Data.SqlClient"
esb.ProviderConnectionString = connString
' Generate the full string and return it
Return esb.ToString()
End Function
Usage:
Using context = New MyEntity("entityConnStr")
Public connection variable string:
Public entityConnStr As String
I think you should remove the quotes when you pass the connection string to the constructor. You want to use the variable contents, not the variable name.
Use this:
Using context = New MyEntity(entityConnStr)
Instead of this:
Using context = New MyEntity("entityConnStr")

Alternative method to save data than my.settings for VB.Net

I need to save approximately 1600 different pieces of information for a windows forms application.
What is the best way to go about this? For example some of the user settings would look like this measuringItem1Name, measuringItem1Equation, measuringItem1Enabled, measuringItem1Offset, measuringItem2Name...
I have looked into my.settings, however, I found that it would be very tedious and error prone to type out every user setting.
It seems like the best solution would be to have a type of table that can access each individual cell and edit that information.
Does something like this exist? If so, how would it be implemented with VB.Net?
I'd recommend creating a class with properties to hold your settings. Mark the class as serializable. Then serialize and deserialize an instance of this class as application startup / shutdown to the user's profile directory. This will allow you to have strongly typed settings that save separately per user if you ever need that option. It will also allow you to easily set default values for the settings.
You can make the "settings" class as deep as you want, with properties made up of other sub setting classes or lists of settings by index. It's a very powerful pattern.
For example:
settings.MeasuringItem(1).Enabled = True
settings.MeasuringItem(1).Equation = "1+1"
settings.MeasuringItem(1).Offset = 15
settings.MeasuringItem(2).Enabled = True
settings.MeasuringItem(2).Equation = "1+1"
settings.MeasuringItem(2).Offset = 15
Settings is the main settings class with a Generic.List(of MeasuringItem) property on it. MeasuingItem is another class with .Enabled As Boolean, .Equation as string , etc properties on it.
I haven't really checked this for errors. You will want to do a lot more safety checking, but this should get you started.
<Serializable>
Public Class Settings
Public Sub New()
_measuringItems = New List(Of MeasuringItem)
End Sub
Private _measuringItems As List(Of MeasuringItem)
Public ReadOnly Property MeasuringItems As List(Of MeasuringItem)
Get
Return _measuringItems
End Get
End Property
Public Sub Save()
Using s As New IO.FileStream("Path to save", IO.FileMode.Create)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter.Serialize(s, Me)
End Using
End Sub
Public Shared Function Load() As Settings
Dim settings As Settings = Nothing
Using s As New IO.FileStream("Path to load", IO.FileMode.Open)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
settings = CType(formatter.Deserialize(s), ServerHost.Settings)
End Using
Return settings
End Function
End Class
<Serializable>
Public Class MeasuringItem
Public Property Enabled As Boolean = True 'These are the default values
Public Property Equation As String = "1+1"
Public Property Offset As Integer = 15
End Class
A quick example of usage:
'Create a completely new settings instance with 100 measuring items
Dim settings1 As Settings = New Settings
For i As Integer = 1 To 100
settings1.MeasuringItems.Add(New MeasuringItem)
Next
settings1.MeasuringItems(0).Enabled = False
settings1.MeasuringItems(5).Equation = "testing"
'Save it
settings1.Save()
'load the saved settings
Dim settings2 As Settings = Settings.Load
I would recommend using a database to store the values in. I don't know your operating environment (for example, I have several enterprise SQL servers available for my use.) You could go with SQL Express Edition, which is free. SQL Lite is an option as well. You could even use Access, but that requires MS Office be installed.
If you don't want to go the database route, you can create a settings class, and make the class serializable . This will serialize the object to either a binary file or a XML file (depending on how you set it up.) If you want to go this approach, I'll post the code for you.

Entity Framework: multiple DB having the same schema

I have just created an ASP.NET MVC 4 & WebAPI project. After that I have added .edmx data source to project.
I have multiple databases with the same schema. Dynamically I want to replace connection string using default constructor provided in EF.
But in Model1.Designer.cs, every time I get error like "Member with same signature already declared".
I'm unable to solve this problem.
Yes, it works! All you need to change is the connection string.
And I have just tested it in order to satisfy my own curiosity.
Here are the steps that I took:
Take an existing database and create a model for it.
Create a new empty database.
In SQL Management Studio right click the first database -> Tasks -> Export Data. Export all it's data to the newly created database.
Remove some records from the second database.
Write this code:
TMS_MiscEntities db = new TMS_MiscEntities();
TMS_MiscEntities dbCopy = new TMS_MiscEntities();
dbCopy.Database.Connection.ConnectionString = db.Database.Connection.ConnectionString.Replace("initial catalog=TMS_Misc", "initial catalog=TMS_Misc_new");
Response.Write(string.Format("DB 1 records: {0}<br/>", db.ZipCodes.Count()));
Response.Write(string.Format("DB 2 records: {0}<br/>", dbCopy.ZipCodes.Count()));
Check results:
DB 1 records: 869164
DB 2 records: 868709
Conclude that it works :)
This is how my connection string looks:
<add name="TMS_MiscEntities" connectionString="metadata=res://*/DbModel.csdl|res://*/DbModel.ssdl|res://*/DbModel.msl;provider=System.Data.SqlClient;provider connection string="data source=ws2008;initial catalog=TMS_Misc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
I'm using Entity Framework 6.1.3. I have added a constructor to my DbContext that takes a string parameter. This string can be the name of the connection stored in your App.config or a full connection string. Something like this:
public partial class MyDBContext : DbContext
{
public MyDBContext(string connectionString)
: base(connectionString)
{
}
// DbSets, OnModelCreating, etc
}
In my case, I manage a multi-tenant application and I use a ContextFactory to build the proper connection string and return my initialized context.
public class ContextFactory
{
public MyDbContext GetContext()
{
string connectionString;
// do some stuff here
return new MyDbContext(connectionString);
}
}

Querying Access database vb.net

I am trying to figure out how to query an access database to take info from the database and put it into a class array. I know how to do some querying using basic LINQ, but not sure how to do it the way I want it to. I have a character class I am using, I am storing all the character traits in a database. I want to query the database and add that info to a class array. Any help would be greatly appreciated.
Here at the class private variables which are the same traits in the database:
Public Class Character
Private _strName As String
Private _intLevel As Integer
Private _intHealth As Integer
Private _intDamage As Integer
Private _intScore As Integer
First of all. I have to say this is not a good question.
Actually what you are saying is...: Please build me the dataconnections for this acces database and a visual studio form.
But because I'm in a good mood, That's what I did for you.
I created a little database file in acces.I saved it into a folder called database. Which is located in the project directory.
I made 2 classes. 1 CharactersForm, and 2 Gateway
the gateway is to make an connection to your Acces database.
The CharactersForm is the form what you see when you debug
Here is the CharactersForm class.
Make sure you drag and drop a listbox and a button
Call the listbox ListBoxCharacters.
Call the button InladenButton. Or call it whatever you want, But make sure it will fit the code given.
Option Explicit On
Option Strict On
Option Infer On
Public Class CharactersForm
Dim _CharactersGateway As New Gateway
Private Sub InladenButton_Click(sender As Object, e As EventArgs) Handles InladenButton.Click
_CharactersGateway.LoadDataTableCharacters()
ListBoxCharacters.DataSource = _CharactersGateway.CharactersDataTable
ListBoxCharacters.DisplayMember = "Name"
End Sub
End Class
The Gateway Class ( to make connection to your acces file)
If somehow visual studio cannot reach the database then it means the connectionstring is not correct. Make sure the connection string is exactly as where your accesfile.accdb is located
Option Explicit On
Option Strict On
Option Infer On
Imports System.IO
Imports System.Data.OleDb
Public Class Gateway
Public Shared AppPath As String = Application.StartupPath()
'\Debug
Public Shared DirectoryUp1 As String = Path.GetDirectoryName(AppPath)
'\bin
Public Shared DirectoryUp2 As String = Path.GetDirectoryName(DirectoryUp1)
'\CharactersStackOverFlow
Public Shared DirectoryUp3 As String = Path.GetDirectoryName(DirectoryUp2)
'\CharactersStackOverFlow
Public Shared AccesDatabaseFilePath As String = DirectoryUp3 & "\DataBase\Characters.accdb"
Dim ConnectionString As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & AccesDatabaseFilePath)
Public Property DataSetCharacters As New DataSet
Public ReadOnly Property CharactersDataTable As DataTable
Get
CharactersDataTable = DataSetCharacters.Tables("Characters")
End Get
End Property
Public CharactersDataAdapter As OleDbDataAdapter
Public Sub LoadDataTableCharacters()
CharactersDataAdapter = New OleDbDataAdapter("SELECT * FROM Characters", ConnectionString)
CharactersDataAdapter.Fill(DataSetCharacters, "Characters")
Dim kamersCommandBuilder As New OleDbCommandBuilder(CharactersDataAdapter)
End Sub
End Class
This is as Yuriy Galanter said. You need the OleDbConnection.
If you are using office 2010 and you get thsi warning ::
'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
You need to download the 2007 Office System Driver. It works also for 2010 don't ask me why.
Type '2007 Office System Driver' in google. And from microsoft you can download and install it.
ALL DONE!
if you run the program and press the load button. The names of your characters will come in the listbox.
If you want to put values of the fields of the database in variables. Then it's just about making a for loop and put them in dimmed variables. That is something I'm sure you can do yourself. The connection to the database and the loading in the program is already sorted out now!
Good luck
Jonathan

Resources