Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 实体框架6.10和数据库优先使用硬编码连接字符串_Vb.net_Entity Framework_Connection String - Fatal编程技术网

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

Vb.net 实体框架6.10和数据库优先使用硬编码连接字符串,vb.net,entity-framework,connection-string,Vb.net,Entity Framework,Connection String,我正在尝试创建一些库,这些库将从公共外部源中提取实体框架的连接字符串。看了这个问题的答案后,我写了下面的代码: 但是当我执行这段代码时,我最终得到一个错误,它无法在app.config文件中找到连接字符串。我错过了什么 更新: 我发现我的实体对象是我的dbContext(即对象上下文),但自动生成的代码如下所示: '------------------------------------------------------------------------------ ' <auto-

我正在尝试创建一些库,这些库将从公共外部源中提取实体框架的连接字符串。看了这个问题的答案后,我写了下面的代码:

但是当我执行这段代码时,我最终得到一个错误,它无法在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
'------------------------------------------------------------------------------
' 
'此代码是从模板生成的。
'
'手动更改此文件可能会导致应用程序出现意外行为。
'如果重新生成代码,将覆盖对此文件的手动更改。
' 
'------------------------------------------------------------------------------
导入系统
导入System.Data.Entity
导入System.Data.Entity.Infrastructure
部分公共类SoftwarePlatformEntities
继承DbContext
公共分新()
MyBase.New(“名称=SoftwarePlatformEntities”)
端接头
模型创建时受保护的覆盖子项(modelBuilder作为DbModelBuilder)
抛出新代码FirstException()
端接头
公共属性EventLogs()作为(EventLog的)DbSet
公共财产监控代理()作为(监控代理的)数据库集
公共属性NetworkMonitors()作为数据库集(属于NetworkMonitor)
末级

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

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

这是用c#写的,但说明了同样的事情

public partial class SoftwarePlatformEntities
{
    public SoftwarePlatformEntities(string nameOrConnectionString)
        :base(nameOrConnectionString)
    {
    }
}
现在可以使用新的构造函数了

using(var ctx = new SoftwarePlatformEntities("metadata=res:// ..."))
{
}
我的解决方案:

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发布在这里。

有趣的是,我必须将其与上面的方法放在一个单独的代码文件中才能使其正常工作。
Partial Public Class SoftwarePlatformEntities
    Inherits System.Data.Entity.DbContext
    Public Sub New(nameOrConnectionString As String)
        MyBase.New(nameOrConnectionString)
    End Sub
End Class