问题
I'm trying to create libraries that will pull the connection string for entity framework from a common external source. I made the code below after looking at the answer to this question: 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
But when I execute this code, I end up getting an error that it cannot find the connection string in the app.config file. What am I missing?
Update:
I figured out that my entity object was my dbContext (ie object context), but the auto-generated code is as follows:
'------------------------------------------------------------------------------
' <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
Still need to figure out how to either make the partial class not point to an entity container defined in app.config, create the entity container, or override the partial class. The code above has been updated.
回答1:
Since it is a partial class you can write another partial class to be built into the same class at compile time. Just make sure they are in the same namespace so that they are considered the same class.
This is in c# but demonstrates the same thing.
public partial class SoftwarePlatformEntities
{
public SoftwarePlatformEntities(string nameOrConnectionString)
:base(nameOrConnectionString)
{
}
}
Now you can use the new constructor
using(var ctx = new SoftwarePlatformEntities("metadata=res:// ..."))
{
}
回答2:
My solution:
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
Separate code file:
Partial Public Class SoftwarePlatformEntities
Inherits System.Data.Entity.DbContext
Public Sub New(nameOrConnectionString As String)
MyBase.New(nameOrConnectionString)
End Sub
End Class
This was extracted from the question and posted here on the OP's behalf.
来源:https://stackoverflow.com/questions/23656542/entity-framework-6-10-and-database-first-with-hard-coded-connection-string