gpt4 book ai didi

database - Entity Framework 4.1 - 交换数据库

转载 作者:搜寻专家 更新时间:2023-10-30 20:31:18 24 4
gpt4 key购买 nike

从我读过的所有内容来看,EntityFramework 应该是蜜蜂膝盖,您可以使用 CodeFirst 从 POCO 生成实体。惊人的!我已经这样做了,我让 EntityFramework 默认行为做它的事情,现在我被困在墙上。

我读过的关于 EntityFramework 和 MVC3 应用程序的所有内容,您只需更改 web.config 中的 DBContext 连接字符串,它应该自动感知、清理它缓存和重新生成的元数据,以及调用默认种子或该上下文的自定义种子方法。我不敢苟同。

我有一个非常简单的数据库上下文类:

namespace AwesomeApp.Models
{
public class MyContext : DbContext
{
public DbSet<Sneeze> Sneeze { get; set; }
}
}

在花了几个冲刺将模型就位、使用 Controller 和 View 后,我准备好迁移到使用 SQLServer Express 2008 的暂存环境。所以我问了谷歌先生如何做到这一点,它公然在此博客文章中指出,您将连接字符串设置为实体上下文作为名称,其他一切都应该到位。

<connectionStrings>
<add name="MyContext"
connectionString="Server=server;Database=awesome_sauce;User ID=noob;Password=noob;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>

我称之为恶作剧,因为它没有在我列出的数据库中创建表,并且仍在访问默认数据库。这给我留下了 2 个问题,我很肯定这是因为我的无知

  1. Entity Framework 默认调用的 SQL Server Express(或者是 CE)的默认数据库在哪里?
  2. 更新连接时,程序员是否需要更多交互才能实现交换数据库的行为?

最佳答案

这是我使用 SQLite 数据库动态更改路径的示例...可以对任何 SQL 采用相同的逻辑。我把它放在我的 WPF 应用程序的 application.xaml 中(所以很可能放在你的 application_start 中)

' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.

Public Sub New()

'Attempt to load the db file
Dim dbFile As New IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory & "Files\database.s3db")

'Loop till we get liteEntities
For Each item As System.Configuration.ConnectionStringSettings In ConfigurationManager.ConnectionStrings
If item.Name <> "liteEntities" Then Continue For

'Allow us to update this ite
Dim fi = GetType(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)
fi.SetValue(item, False)

'Update it
item.ConnectionString = Replace(item.ConnectionString, "C:\db\Files\database.s3db", dbFile.FullName)

Next


End Sub

关于database - Entity Framework 4.1 - 交换数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6196073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com