【发布时间】: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