实体框架6.10和数据库优先使用硬编码的连接字符串 [英] Entity Framework 6.10 and database first with hard-coded connection string

查看:112
本文介绍了实体框架6.10和数据库优先使用硬编码的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建库,以从公共外部源获取实体框架的连接字符串。在查看此问题的答案后,我在下面编写了代码:如何在没有App.config的情况下使用实体框架

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

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

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?

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

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

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

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.

推荐答案

由于它是一个局部类,因此您可以编写另一个要构建到该局部类中的局部类。在编译时上课。只需确保它们在相同的名称空间中,以便将它们视为相同的类即可。

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.

这在c#中却演示了相同的事情。

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:// ..."))
{
}

这篇关于实体框架6.10和数据库优先使用硬编码的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆