VS2013 update 4, MVC5, elmah.mvc (same as elmah), VB/C#
VS2013更新4,MVC5,elmah.mvc(与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:
以下代码是标准MVC模板的一部分,它是从代码中设置数据库连接以访问SQL数据库的一部分:
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:
我们还需要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.
我添加了elmah连接字符串,因为它与我的问题相关。
Then in each location where needed, the following definition is added and we can access the database tables using db.< tablename>...:
然后在需要的每个位置,添加以下定义,我们可以使用db。
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.
我想编写一些方法来处理和使用elmah数据表,但它与ApplicationContext不同。我不太明白我会写什么来设置与elmah表的连接,因为我没有真正设置它,它来自elmah的dll。
1 个解决方案
#1
0
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.
虽然保持错误记录是有道理的,但在开发期间我想清除Elmah日志。尽管对大多数人来说可能是微不足道的,但要想出来这是一个很好的练习。我已经习惯了先编码,我忘了EF也先做数据库。
In VS2013 update 4 the following Wizard made this task pretty easy.
在VS2013更新4中,以下向导使此任务变得非常简单。
1) Project > Add New Item > Data > ADO.NET Entity Data Model
1)项目>添加新项>数据> ADO.NET实体数据模型
2) Give the model a name (With great creativity I used 'elmah') > Click Add
2)给模型一个名字(我用'elmah'创造了极大的创造力)>点击添加
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.
3)这是非常酷的地方。我不确定它何时被添加,但EF框架现在包含一个名为Code First From Database的新选择。点击它。
4) Select the elmah connection string
4)选择elmah连接字符串
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
注意:可以选择在web.config中保存连接设置。这只是创建一个重复的连接字符串。保持检查或不检查,但使用现有的字符串是好的。我没有选择该选项,并且web.config保持不变。但是,不使用该选项意味着必须在上下文定义中更新连接字符串名称>单击“下一步”
5) Click on Tables to load the Elmah table, leave the defaults as is > Click Finish
5)单击Tables以加载Elmah表,保留默认值>单击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.
该向导创建2个文件并将它们放在项目中。一个是由数据表名称命名的,即ELMAH_Error;这是模型。另一个是设置背景的一个新的类。如果选择了“保存连接”选项,则将修改web.config以添加新的连接字符串。注意:为方便起见,我将2个类合并为一个文件(可能需要添加一些使用或导入),如果使用现有连接字符串,则必须更新连接字符串名称,如下所示。
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:
6)所有这些都自动完成(如果需要,连接字符串名称除外),所需的只是编写所需的代码。我想不时清空elmah日志并使用以下内容:
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.
显然我有一个名为ErrorTesting的方法,该函数返回并从同一页面上的链接访问此方法。
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.
希望这对某人有用。考虑到我经常从这个网站获得的令人敬畏的大师帮助,这是我可以回馈的唯一方式。
#1
0
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.
虽然保持错误记录是有道理的,但在开发期间我想清除Elmah日志。尽管对大多数人来说可能是微不足道的,但要想出来这是一个很好的练习。我已经习惯了先编码,我忘了EF也先做数据库。
In VS2013 update 4 the following Wizard made this task pretty easy.
在VS2013更新4中,以下向导使此任务变得非常简单。
1) Project > Add New Item > Data > ADO.NET Entity Data Model
1)项目>添加新项>数据> ADO.NET实体数据模型
2) Give the model a name (With great creativity I used 'elmah') > Click Add
2)给模型一个名字(我用'elmah'创造了极大的创造力)>点击添加
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.
3)这是非常酷的地方。我不确定它何时被添加,但EF框架现在包含一个名为Code First From Database的新选择。点击它。
4) Select the elmah connection string
4)选择elmah连接字符串
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
注意:可以选择在web.config中保存连接设置。这只是创建一个重复的连接字符串。保持检查或不检查,但使用现有的字符串是好的。我没有选择该选项,并且web.config保持不变。但是,不使用该选项意味着必须在上下文定义中更新连接字符串名称>单击“下一步”
5) Click on Tables to load the Elmah table, leave the defaults as is > Click Finish
5)单击Tables以加载Elmah表,保留默认值>单击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.
该向导创建2个文件并将它们放在项目中。一个是由数据表名称命名的,即ELMAH_Error;这是模型。另一个是设置背景的一个新的类。如果选择了“保存连接”选项,则将修改web.config以添加新的连接字符串。注意:为方便起见,我将2个类合并为一个文件(可能需要添加一些使用或导入),如果使用现有连接字符串,则必须更新连接字符串名称,如下所示。
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:
6)所有这些都自动完成(如果需要,连接字符串名称除外),所需的只是编写所需的代码。我想不时清空elmah日志并使用以下内容:
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.
显然我有一个名为ErrorTesting的方法,该函数返回并从同一页面上的链接访问此方法。
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.
希望这对某人有用。考虑到我经常从这个网站获得的令人敬畏的大师帮助,这是我可以回馈的唯一方式。