的SQL Server CE的Code First迁移问题(SQL Server CE Code F

2019-08-31 16:56发布

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.

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

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

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...

My connection string:

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

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; }
}

Then run Add-Migration InitialMigration again and get:

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");
    }
}

After running Update-Database I see:

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

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 ]

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

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...

Another little piece of information:

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.

I'm using VS 2012 and EF 5.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. :((

Thank you! david

Answer 1:

所以,问题是该解决方案创建一个独立的.sdf文件在这里:

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

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

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

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

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

用这种方式唯一的事情就是我的Logo.sdf项目文件(这是得到复制到调试“是否有更新”),现在已经完全忽略。 所有迁移将在调试文件运行..这是件好事太....

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



文章来源: SQL Server CE Code First migrations problems