SQL Server CE的code首先迁移问题问题、Server、SQL、code

2023-09-03 06:47:59 作者:女生

我有一大堆的问题,尝试启用(code一)迁移为我的SQL Server Compact 4.0数据库为我的桌面.NET应用程序。

I have a bunch of problems trying to enable (code-first) migrations for my SQL Server Compact 4.0 database for my desktop .NET app.

启用-迁移不工作,并在目录迁移创建。之后,当我尝试运行添加迁移InitialMigration ,我得到:

Enable-Migrations does work and the directory Migrations is created. After that when I try to run Add-Migration InitialMigration, I get:

访问数据库文件是不允许的。 [1914年,文件名= Logo.sdf,SeCreateFile]

Access to the database file is not allowed. [ 1914,File name = Logo.sdf,SeCreateFile ]

这是第一个问题,但我解决它通过运行Visual Studio以管理员身份......不一样,解决方案,也不知道以后是否在生产中,将工作没有应用程序正在运行管理模式。我让这个问题放在一边,现在...

This is the first problem, but I solved it by running Visual Studio as Administrator... don't like that solution and also don't know if later in production it will work without the app being run in Admin mode. I let that problem aside for now...

我的连接字符串:

<add name="LogoContext" 
     connectionString="Data Source=Logo.sdf" 
     providerName="System.Data.SqlServerCE.4.0"/>`

所以,运行后添加迁移InitialMigration 在管理员模式下,我得到一个空的迁移......没关系。然后,我删除了迁移,并添加一个新的类:

So after running Add-Migration InitialMigration in Administrator mode I get an empty migration... that's ok. Then I delete the migration and add a new class:

using System;
using System.Collections.Generic;

public class Blog
{
    public int ID { get; set; }
    public string Title { get; set; }
}

我添加一个引用到上下文类:

I add a reference to the context class:

public class LogoContext : DbContext
{
    public DbSet<Word> Words { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Blog> Blogs { get; set; }
}

然后运行添加迁移InitialMigration 再次获得:

public partial class InitialMigration : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.Blogs",
            c => new
                {
                    ID = c.Int(nullable: false, identity: true),
                    Content = c.String(maxLength: 4000),
                })
            .PrimaryKey(t => t.ID);
    }

    public override void Down()
    {
        DropTable("dbo.Blogs");
    }
}

运行后更新 - 数据库我明白了:

Applying code-based migrations: [201304211225255_InitialMigration].
Applying code-based migration: 201304211225255_InitialMigration.
Running Seed method.

现在问题出现了 - 在我的服务器资源管理器我检查数据库 Logo.sdf 和它的不包含表博客!我甚至尝试从我的应用程序运行此code:

Now the problem appears - in my Server Explorer I examine the database Logo.sdf and it does not include the table Blogs! I even try to run this code from my app:

var db = new LogoContext();
db.Posts.Add(new Blog { Title= "moo" });
db.SaveChanges();

要检查也许我的服务器资源管理器没有显示表..但我得到一个异常:

to check if maybe my Server Explorer isn't showing the table.. but I get an exception:

指定的表不存在。 [博客]

The specified table does not exist. [ Blogs ]

所以,迁移显然没有被应用到我的 Logo.sdf 文件:(

So the migrations are obviously not being applied to my Logo.sdf file :(

如果我删除的app.config 连接字符串,假定连接到SQL Server的防爆preSS本地实例。还有它完美的作品!当我审视与SQL Server Management Studio中的数据库中,我看到了新的博客表,也是一个系统表有关迁移的元数据...

If I remove the connection string from app.config, the connection to a local instance of SQL Server Express is assumed. And there it works flawlessly!! When I examine the database with SQL Server Management Studio, I see the new Blogs table and also a system table for metadata about migrations...

另一个小一条信息:

当我尝试运行更新 - 数据库再次,我得到无待code为基础的迁移。并告诉我说,有些数据被保存到 Logo.sdf 毕竟......至少约迁徙一些元数据,但我仍然无法看到Server的表资源管理器。

When I try to run Update-Database again, I get "No pending code-based migrations." and that tells me that some data is being saved to Logo.sdf after all... at least some metadata about migrations, but still I can't see that table in Server Explorer.

我使用VS 2012和EF 5.0。

I'm using VS 2012 and EF 5.0.

请帮助我了解这个......在我看来,什么是严重的错误,因为它只是在SQL Server防爆preSS实例的工作,而不是与SQL Server CE 4.0。 :((

Please help me understand this... It looks to me that something is seriously wrong because it just works with SQL Server Express instance, but not with SQL Server CE 4.0. :((

感谢您! 大卫

推荐答案

所以,问题是,该解决方案中创建一个单独的.sdf文件位置:

So the problem was that the solution created a separate .sdf file here:

C:\ Program Files文件(x86)的\微软的Visual Studio 11.0 \ Common7 \ IDE \ Logo.sdf

SQL Server 2008可以安装在win7 64位的系统上吗

"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Logo.sdf"

这是意外和奇怪恕我直言...

Which was unexpected and strange IMHO...

我结束了使用此连接字符串:

I ended up using this connection string:

<add name="LogoContext" connectionString="Data Source=|DataDirectory|\Logo.sdf" providerName="System.Data.SqlServerCE.4.0"/>

此引用斌/调试/ Logo.sdf和发展过程中它的工作原理,并分别运行.exe文件时。

This references bin/Debug/Logo.sdf and it works during development and when running a .exe separately.

通过这种方式唯一的一点是,我的Logo.sdf项目文件(这是得到复制到调试是否有更新)现在已经完全被忽略。所有的迁移将在调试文件运行......这大概也很好......

The only thing with this way is that my Logo.sdf project file (which was getting copied to Debug "if newer") is now completely ignored. All the migrations will be run on the Debug file.. That's probably good too....

感谢名单埃里克的暗示! 大卫

Thanx Erik for the hint! david