实体框架6.10和数据库首先使用硬编码连接字符串

时间:2014-05-14 13:48:13

标签: vb.net entity-framework connection-string

我正在尝试创建库,以便从公共外部源获取实体框架的连接字符串。在查看了这个问题的答案之后,我制作了以下代码:How can l use Entity Framework without App.config

Dim myConnectionString As String = "metadata=res://*/SoftwarePlatformModel.SoftwarePlatformModel.csdl|res://*/SoftwarePlatformModel.SoftwarePlatformModel.ssdl|res://*/SoftwarePlatformModel.SoftwarePlatformModel.msl;provider=System.Data.SqlClient;provider connection string=""data source=.\SQLEXPRESS2012;initial catalog=SoftwarePlatform;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"""
Using db = New SoftwarePlatform.SQL.SoftwarePlatformEntities
    db.EventLogs.Add(EventLog)
    db.SaveChanges()
End Using

但是当我执行这段代码时,我最终得到一个错误,它无法在app.config文件中找到连接字符串。我错过了什么?


更新

我发现我的实体对象是我的dbContext(即对象上下文),但是自动生成的代码如下:

'------------------------------------------------------------------------------
' <auto-generated>
'    This code was generated from a template.
'
'    Manual changes to this file may cause unexpected behavior in your application.
'    Manual changes to this file will be overwritten if the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class SoftwarePlatformEntities
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=SoftwarePlatformEntities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Property EventLogs() As DbSet(Of EventLog)
    Public Property MonitoringAgents() As DbSet(Of MonitoringAgent)
    Public Property NetworkMonitors() As DbSet(Of NetworkMonitor)

End Class

仍然需要弄清楚如何使部分类不指向app.config中定义的实体容器,创建实体容器或覆盖部分类。上面的代码已更新。

2 个答案:

答案 0 :(得分:1)

因为它是一个部分类,所以你可以在编译时编写另一个部分类来构建到同一个类中。只需确保它们位于相同的名称空间中,以便它们被视为同一个类。

这是在c#中,但演示了同样的事情。

public partial class SoftwarePlatformEntities
{
    public SoftwarePlatformEntities(string nameOrConnectionString)
        :base(nameOrConnectionString)
    {
    }
}

现在您可以使用新的构造函数

using(var ctx = new SoftwarePlatformEntities("metadata=res:// ..."))
{
}

答案 1 :(得分:0)

我的解决方案:

Dim myConnectionString As String = "metadata=res://*/SoftwarePlatformModel.SoftwarePlatformModel.csdl|res://*/SoftwarePlatformModel.SoftwarePlatformModel.ssdl|res://*/SoftwarePlatformModel.SoftwarePlatformModel.msl;provider=System.Data.SqlClient;provider connection string=""data source=.\SQLEXPRESS2012;initial catalog=SoftwarePlatform;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"""
Using db = New SoftwarePlatform.SQL.SoftwarePlatformEntities(myConnectionString)
    db.EventLogs.Add(EventLog)
    db.SaveChanges()
End Using

单独的代码文件:

Partial Public Class SoftwarePlatformEntities
    Inherits System.Data.Entity.DbContext
    Public Sub New(nameOrConnectionString As String)
        MyBase.New(nameOrConnectionString)
    End Sub
End Class

这是从问题中提取的,并代表OP发布在此处。