简体   繁体   English

代码优先迁移中的二进制字段

[英]Binary fields in code-first migrations

Regards, 问候,

I have problems with migrations and binary fields... 我在迁移和二进制字段方面遇到问题...

My initial migration looks like this (I had an existing database): 我的初始迁移如下所示(我有一个现有数据库):

public override void Up()
{
    CreateTable("dbo.Words", c => new
    {
        ID = c.Int(nullable: false, identity: true),
        Name = c.String(maxLength: 4000),
        Image = c.Binary(),
        Audio = c.Binary(),
    }).PrimaryKey(t => t.ID);
}

And the Entity class is here: 实体类在这里:

public class Word
{
    public int ID { get; set; }
    public string Name { get; set; }
    [MaxLength]
    public byte[] Image { get; set; }
    [MaxLength]
    public byte[] Audio { get; set; }
}

I have automatic migrations turned on. 我已启用自动迁移。 Now if I drop all the tables from the database and run "Update-Database" I get: 现在,如果从数据库中删除所有表并运行“ Update-Database”,则会得到:

Applying code-based migration: 201304211813502_InitialMigration.
Applying automatic migration: 201304212024538_AutomaticMigration.
System.Data.SqlServerCe.SqlCeException (0x80004005): Cannot alter column of type NTEXT or IMAGE [ Column Name = Image ]

So it looks like something is not in sync.. I run "Add-Migration AutoMigration" to see what entity framework had in mind it has to still be updated... The file I get is here: 所以看起来好像有些不同步。。我运行“添加迁移自动迁移”,以了解必须更新的实体框架。我得到的文件在这里:

public partial class AutoMigration: DbMigration
{
    public override void Up()
    {
        AlterColumn("dbo.Words", "Image", c => c.Binary());
        AlterColumn("dbo.Words", "Audio", c => c.Binary());
    }

    public override void Down()
    {
        AlterColumn("dbo.Words", "Audio", c => c.Binary(maxLength: 4000));
        AlterColumn("dbo.Words", "Image", c => c.Binary(maxLength: 4000));
    }
}

So... from this it looks like EF thinks Image and Audio fields are Binary(maxLength: 4000).. but they're not supposed to be!! 所以...从这看来,EF认为“图像”和“音频”字段是Binary(maxLength:4000)..但它们不应该是这样! (because in the initial migration they are Binary() ). (因为在初始迁移中它们是Binary() )。

So I'm stuck and cannot get up-to-date with my migrations... and I need MaxLength (unlimited) binary fields... 因此,我陷入了困境,无法及时了解自己的迁移信息,而且我需要MaxLength(无限)二进制字段...

What could be going on here? 这可能是怎么回事? thank you! 谢谢! david 大卫

I finally managed to upgrade the project to EF 6.0.0 Alpha3 ... I had to fix a lot of small problems and also upgrade NuGet to 2.5 RC. 我终于设法将项目升级到EF 6.0.0 Alpha3 ...我不得不解决很多小问题,还必须将NuGet升级到2.5 RC。 Now it works.. not sure what I did but most likely it was just an upgrade to EF 6 现在它可以工作了..不知道我做了什么,但是很可能这只是对EF 6的升级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM