【问题标题】:Entity Framework 6.10 and database first with hard-coded connection string实体框架 6.10 和数据库首先使用硬编码的连接字符串
【发布时间】:2014-05-14 13:48:13
【问题描述】:

我正在尝试创建库,这些库将从公共外部源中提取实体框架的连接字符串。看了这个问题的答案后,我做了下面的代码: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 中定义的实体容器,创建实体容器,或覆盖分部类。以上代码已更新。

【问题讨论】:

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


    【解决方案1】:

    由于它是一个分部类,您可以编写另一个分部类以在编译时构建到同一个类中。只需确保它们位于同一个命名空间中,以便将它们视为同一个类。

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

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

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

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

    【讨论】:

    • 作为一个有趣的说明,我必须将它放在与我上面的方法不同的代码文件中才能使其工作。
    【解决方案2】:

    我的解决方案:

    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 发布在此处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      相关资源
      最近更新 更多