mysql - 从 Entity Framework 6 Code First MSSQL 迁移到 MySQL

标签 mysql sql-server entity-framework entity-framework-6

我曾多次在 MSSQL 中首先使用 EF Code,但这是我第一次在 MySQL 中使用它。

我在 AWS 上分配了一个带有用户名和密码的 MySQL 数据库,使用 MySQL Workbench 我可以看到数据库并创建/删除表......快乐的日子。

我的 Code First 解决方案出现问题,其中包管理器在尝试创建表时不报告任何错误,但表在工作台中不可见......他们要去某个地方,但我不知道哪里...

我启用了迁移,他们在我的表名前加上

这是我的连接字符串:

<connectionStrings>
<add name="MyContext" providerName="MySql.Data.MySqlClient"
    connectionString="server=mysql-portal.blahblahblah.us-somewhere-1.rds.amazonaws.com;port=3306;database=mydatabasename;uid=myusername;password=mypassword"/>
</connectionStrings>

这是我的 DbContext:

public class MyContext : DbContext
{
    public MyContext() : base("MyContext")
    {
        Database.SetInitializer<MyContext>(new MyDbInitializer());
    }

    public virtual DbSet<AgentData> AgentDatas { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.HasDefaultSchema("mydatabasename");
    }
}
public class MyDbInitializer : CreateDatabaseIfNotExists<MyContext>
{
    protected override void Seed(MyContext context)
    {
        base.Seed(context);
    }
}

当我添加迁移时,代码看起来是正确的:

CreateTable(
            "mydatabasename.AgentData",
            c => new
                {
                    Id = c.Long(nullable: false, identity: true),
                    State = c.String(),
                    AgentName = c.String(),
                    AgentLogin = c.String(),
                    AgentPassword = c.String(),
                    NAID = c.String(),
                    License = c.String(),
                    Proxy = c.String(),
                    Enabled = c.Boolean(nullable: false),
                    American_States_id = c.Long(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("mydatabasename.American_States", t => t.American_States_id)
            .Index(t => t.American_States_id);

正如我所说,当我运行此迁移并更新数据库时,我没有收到任何错误。

但是我在 Workbench 中看不到该表,如果我尝试执行以下操作,它会说该表不存在...

public static async Task<List<Model.AgentData>> GetAllAgents()
    {
        using (MyContext db = new MyContext())
        {
            return await db.AgentDatas.ToListAsync();
        }
    }

我很难过...我没有任何其他数据库的权限...所以他们要去哪里以及我做错了什么,因为他们没有出现在指定的正确数据库中:

modelBuilder.HasDefaultSchema("mydatabasename");

更新:

奇怪的事情还在继续。

我找不到上述方法不起作用的原因。所以我决定启动一个完全干净的解决方案,并在该解决方案中从头开始构建项目。

而且效果很好。我可以看到我的表,并且可以向数据库中添加/删除表。

所以,我想,没问题。我现在将该工作项目添加到我的其他解决方案中。我从我的主要解决方案中删除了旧解决方案。删除了目录。将工作项目复制到主解决方案目录并将其添加到解决方案...

它开始做同样的事情...它报告在包管理器中创建表没有错误,但我在 Workbench 中看不到它们,也无法向它们添加数据。

两个解决方案/项目都在 Visual Studio Community 2017 中。

有效的解决方案是仅包含工作项目的解决方案。不起作用的解决方案中包含多个现有项目。

为什么,当我将一个工作项目导入到解决方案中时,它会停止工作....

有人能猜猜吗?

更新:

这开始看起来更像是 VS2017 中的错误

我刚刚在我的本地 MSSQL 实例中找到了所有丢失的表和数据库!

这就像在多项目解决方案中选择 MSSQL 的本地实例并忽略 App.config 中到 MySQL 数据库的连接字符串!

当我进入“服务”并停止我的本地 SQL Server 实例并尝试执行 Enable-Migrations -Force 时,我收到错误

The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: The system cannot find the file specified

最佳答案

我已经设法通过 2 处更改解决了我的问题

  1. 我在我的 HudHomeContext 类上添加了 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfigu‌ ration))] 作为声明

  2. 我将连接字符串放在我的基本声明中...

现在看起来像这样:

 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfigu‌​ration))]
public class MyContext : DbContext
{
    public MyContext() : base("server=mysql-portal.blahblah.us-blah-1.rds.amazonaws.com;port=3306;database=blahblah;uid=myusername;password=MYPASSWORD")
    {
        Database.SetInitializer<MyContext>(new MyDbInitializer());
    }

关于mysql - 从 Entity Framework 6 Code First MSSQL 迁移到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43953273/

相关文章:

mysql - SQL - 如何使用父表的列名连接外键数据

php - 我应该在 PDO 事务中运行多个 SELECT 以提高性能吗?

sql-server - 在 SQL Server 2005 中使用 FreeTextTable 时初始查询速度缓慢

sql-server - 多对多关系中的多个级联删除路径 (EF 4.1)

java - JPA双向关系

php - 来自列表 id 的 mysql 查询名称

mysql - 为什么带有 varchar 变量的 mysql 查询执行速度极慢?

SQL:WHERE 子句多个条件

sql-server - 什么图像用于通过 bitbucket 云自动将 SSIS 包部署到 SQL Server?

c# - EntityFramework 更新或插入中文或非英文文本