EF CTP 4 数据库名称配置

发布于 2024-10-04 02:38:01 字数 890 浏览 0 评论 0原文

例如,我使用这个连接字符串:

<connectionStrings>    
<add name="PicturesDatabase" 
         connectionString=" Server=.;
                            Database=SomeprojectDatabase;
                            Trusted_Connection=True;"
         providerName="System.Data.SqlClient"/>
<../>

然后我在 Application_Start() 中使用它:

Database.DefaultConnectionFactory =
    new SqlConnectionFactory(ConfigurationManager.
        ConnectionStrings["PicturesDatabase"].ConnectionString);

在我的数据库中,我得到了这个非常奇怪的新数据库: MyAppNamespace.Models.PicturesCatalog 有两个表 dbo.EdmMetadatadbo.Pictures

表很好,但为什么不创建一个名为 PicturesDatabase 的新数据库(如连接字符串)与那些表?

我已经尝试删除此表几次,我确实尝试创建 PicturesDatabase 并使用它...但它仍然生成此 MyAppNamespace.Models.PicturesCatalog。它有什么问题呢?我该如何解决它?

For sample I'm using this connection string :

<connectionStrings>    
<add name="PicturesDatabase" 
         connectionString=" Server=.;
                            Database=SomeprojectDatabase;
                            Trusted_Connection=True;"
         providerName="System.Data.SqlClient"/>
<../>

And then I use it in Application_Start() :

Database.DefaultConnectionFactory =
    new SqlConnectionFactory(ConfigurationManager.
        ConnectionStrings["PicturesDatabase"].ConnectionString);

And in my database I get this really strange new Database :
MyAppNamespace.Models.PicturesCatalog
with two tables dbo.EdmMetadata and dbo.Pictures

Tables are fine, but why doesn't it create a new database named PicturesDatabase (as in connection string) with those tables ?

I've tried dropping this table few times, I did try creating PicturesDatabase and using it... but it still generates this MyAppNamespace.Models.PicturesCatalog. What is the problem with it ? And how do I fix it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-10-11 02:38:01

System.Data.Entity.Infrastruct.SqlConnectionFactory 不适合以这种方式使用。这个连接工厂包含一个构造函数,它允许我们覆盖最终连接字符串的部分,例如用户名、密码和服务器,而不是整个。

例如,我们可以像这样更改数据库服务器:

Database.DefaultConnectionFactory = 
        new SqlConnectionFactory("Server=MyDatabaseServer"); 

您在这里尝试执行的操作可以通过在 app.config 中提供一个与 DbContext 名称​​匹配的连接字符串来轻松完成 - 我假设它是图片目录

<connectionStrings>
   <add name="PicturesCatalog" connectionString="data source=.; 
        Initial Catalog=PicturesDatabase; Trusted_Connection=True;"
        providerName="System.Data.SqlClient" />
</connectionStrings>

System.Data.Entity.Infrastructure.SqlConnectionFactory is not meant to use in this way. This connection factory includes a constructor that allows us to override pieces of the final connection sting, such as username, password and server and not the whole thing.

For example, we can change the Database Server like this:

Database.DefaultConnectionFactory = 
        new SqlConnectionFactory("Server=MyDatabaseServer"); 

What you try to do here can be easily accomplished by providing a connection string in your app.config that matches the name of your DbContext - I assume it's PicturesCatalog:

<connectionStrings>
   <add name="PicturesCatalog" connectionString="data source=.; 
        Initial Catalog=PicturesDatabase; Trusted_Connection=True;"
        providerName="System.Data.SqlClient" />
</connectionStrings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文