简体   繁体   English

EF CodeFirst迁移不起作用

[英]EF CodeFirst migrations not working

I am using EF 6 code first . 我首先使用EF 6代码。 I have changed my DbContext to pass connction string to constructor 我已经更改了我的DbContext以将连接字符串传递给构造函数

public class EFDbContext : DbContext
{
    public EFDbContext(string connectionString)
        : base(connectionString)
    {
        Database.Connection.ConnectionString = connectionString;
    }
    public EFDbContext()
    {
    }

    public DbSet<MC_Users> MCUsers { get; set; }
}

and moved connection string from Web.Config 并从Web.Config移动连接字符串

Everything was working fine. 一切都很好。 But adding one more property to MC_Users, is not effecting the database, i mean the code first is not working. 但是向MC_Users添加一个属性不会影响数据库,我的意思是代码首先不起作用。

 add-migration test    command created a new DbMigration class test
 update-database command applied it successfully

but my database table is not changing 但我的数据库表没有改变

Please help 请帮忙

You are changing local sqlexpress database. 您正在更改本地sqlexpress数据库。 You need to provide custom connection string to Update-Database cmdlet: 您需要为Update-Database cmdlet提供自定义连接字符串:

Update-Database -ConnectionString Update-Database -ConnectionString

http://coding.abel.nu/2012/03/ef-migrations-command-reference/#Update-Database http://coding.abel.nu/2012/03/ef-migrations-command-reference/#Update-Database

Also you can use DbMigrator class to do migration from code without using Update-Database cmdlet. 此外,您可以使用DbMigrator类从代码进行迁移,而无需使用Update-Database cmdlet。 When you use DbMigrator class you need to set dbMigrator.Configuration.TargetDatabase to change default connection string. 使用DbMigrator类时,需要设置dbMigrator.Configuration.TargetDatabase以更改默认连接字符串。

var dbMigrator = new DbMigrator(new Configuration());
dbMigrator.Configuration.TargetDatabase = //New connection settings goes here.
dbMigrator.Configuration.AutomaticMigrationDataLossAllowed = true;
dbMigrator.Update();

http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrator(v=vs.113).aspx http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.dbmigrator(v=vs.113).aspx

In order to make your migration works, you must add connection string to the web.config/app.config of your project that is set up as a "Startup Project". 为了使您的迁移工作,您必须将连接字符串添加到设置为“启动项目”的项目的web.config / app.config中。 After setting these, on the package manager console you should choose DbContext project and try Update-Database method. 设置完成后,在包管理器控制台上,您应该选择DbContext项目并尝试使用Update-Database方法。

Leaving an answer here as I had a similar problem that wasn't solved by the accepted answer: 在这里留下答案,因为我遇到了一个类似的问题,这个问题没有被接受的答案解决:

I had the password wrong in the app.config I was using against a different environment. 我在app.config中输入的密码错误,我使用的是针对不同的环境。 Get-Migrations doesn't give you an error about failed credentials, it just tells you "No migrations have been applied to the target database", which is hugely misleading. Get-Migrations没有提供有关失败凭据的错误,它只是告诉您“没有迁移已应用于目标数据库”,这极具误导性。

Hopefully someone else who comes across this will discover it's as simple as a typo in their connection string! 希望遇到此问题的其他人会发现它就像连接字符串中的拼写错误一样简单!

To the mod who deleted this: 对于删除了这个的mod:

Please don't post identical answers to multiple questions. 请不要在多个问题上发布相同的答案。 Post one good answer, then vote/flag to close the other questions as duplicates. 发布一个好的答案,然后投票/标志将其他问题作为重复项结束。 If the question is not a duplicate, tailor your answers to the question. 如果问题不重复,请定制您对问题的答案。

It's not a duplicate answer from another question; 这不是另一个问题的重复答案; it's an alternate answer to this one. 这是另一个答案。 I had this problem at work, but the accepted solution wasn't the answer for me. 我在工作中遇到了这个问题,但是接受的解决方案不适合我。 This is simply another option for someone to check if they stumble upon this question. 这只是某人检查他们是否偶然发现这个问题的另一种选择。

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

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