c# - EF6 代码优先迁移中的架构名称重复

标签 c# mysql entity-framework entity-framework-migrations

更新数据库正在输出此错误。

Table 'external_service.external_service.products' doesn't exist

这是一些更新数据库详细输出。

Target database is: 'external_service' (DataSource: something,     Provider: MySql.Data.MySqlClient, Origin: Configuration).
Applying explicit migrations: [201503091803430_1.0.1].
Applying explicit migration: 201503091803430_1.0.1.
alter table `external_service.products` drop column `test`

这就是数据库迁移的样子...

public partial class _101 : DbMigration
{
    public override void Up()
    {
        DropColumn("external_service.products", "test");
    }

    public override void Down()
    {
        AddColumn("external_service.products", "test", c => c.Boolean(nullable: false));
    }
}

这是 POCO

// Both of these generate the same thing. :/
//[Table("external_service.products")]
//[Table("products", Schema = "external_service")]
[Table("products")]
public partial class Product
{
    [Column("productid")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long ProductID { get; set; }

    [Column("visible")]
    public bool Visible { get; set; }
}

这是迁移配置

internal sealed class Configuration : DbMigrationsConfiguration<SoundDevices.DataAccessLayer.Context.ExternalServiceContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\ExternalService";
    }

    protected override void Seed(SoundDevices.DataAccessLayer.Context.ExternalServiceContext context)
    {
    }
}

我将 EF 6.1.2 与 MySql.Data.Entity 6.9.6 和 MySql.Data 6.9.6 结合使用

如果我手动从 DbMigration 中删除“external_service”,它会起作用,但这似乎不正确。我错过了什么吗?

最佳答案

解决方案取决于您的命名含义。如果您的数据库名称是 external_service 并且您希望该表名为 products,请指定:

[Table("products")]

这将创建一个具有完全限定名称 external_service.products 的表。在 SQL Server 中,这将是 external_service.dbo.products (或 external_service..products,因为 dbo 是默认架构的名称。

如果您实际上尝试在数据库中名为 external_service 的模式(也称为 external_service)中创建表 products,那么您将使用

[Table("products", Schema = "external_Service")]

这将创建一个完全限定的表名称 external_service.external_service.products

您还提到,在您更改某些内容并重新创建迁移后,它会尝试重命名或移动表。这表明在创建初始迁移时,您尚未删除旧版本。要重新创建初始迁移,请撤消第一个迁移,然后重新创建新迁移:

Update-Database -TargetMigration:0
Add-Migration 1.0.1 -Force

关于c# - EF6 代码优先迁移中的架构名称重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28948890/

相关文章:

mysql - MySQL脚本中显示的方 block 字符

c# - Entity Framework 中的两列外键

c# - 如何在 Realm 中使用 Linq 表达式检查可空类型的空值?

c# - 通过窗口句柄查找进程ID

c# - 如何初始化具有另一个接口(interface)类型的类型参数的接口(interface)

sql - 如何在 Entity Framework 中发送常规内联SQL

entity-framework - Entity Framework : How to return a row from a table with composite keys?

c# - 编辑包含引号的注册表字符串

mysql - 检查mysql中同一单元格中的重复数据

php - 在mysql中保存图像名称?