none
Getting Error in Code First

    Frage

  • When I issue the command "Update-Database" I get the error:

    Column names in each table must be unique. Column name 'EmailServerTypeId' in table 'EmailAccounts' is specified more than once.

    Any clues?  I only see the column once.

    I have the following code:

    namespace EFCodeFirstLib.CodeFirstModels
    {
    
        public class UserConnectionHeartBeat
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public long Id { get; set; }
    
            public User User { get; set; }
    
            
        }
    
        public enum EmailServerType
        {
            POP3 = 1,
            IMAP = 2
        }
    
    
        public class EmailAccount
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public long Id { get; set; }
    
            [Required(ErrorMessage = "Must Be IMAP (2) or POP3 (1)   enums not yet supported in CF")]
            public int EmailServerTypeId { get; set; }
    
            [Required(ErrorMessage = "Server Name Required")]
            public string ServerName { get; set; }
    
            [Required(ErrorMessage = "Username  Required")]
            public string Username { get; set; }
    
            [Required(ErrorMessage = "Password Name Required")]
            public string Password { get; set; }
    
            public int? Port { get; set; }
            public bool? UseSslSocket { get; set; }
    
            
        }
    


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Dienstag, 21. Februar 2012 17:55

Antworten

  • Hi Peter,

    You should put your seed stuff in Configuration.cs file:

            protected override void Seed(MyContext context)
            {
                //  This method will be called after migrating to the latest version.
                //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
                //  to avoid creating duplicate seed data. E.g.
                //
                    context.EmailAccounts.AddOrUpdate(
                    new EmailAccount{ Id=1, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=2, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=3, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=4, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true}
                    );
            }

    Yes, you should create database first then you can enable-migrations for your application. To run Migration stuff, you should use NuGet mannually.
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Montag, 27. Februar 2012 08:31
    Moderator

Alle Antworten

  • Hi Peter,

    Welcome!

    I guess you may add same column in your Migrations, please check it:

    public partial class MyConfig : DbMigration { public override void Up() { AddColumn("EmailAccount", "EmailServerTypeId", c => c.Int); } }

    Remove it!

    Have a nice day.

     

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Mittwoch, 22. Februar 2012 05:50
    Moderator
  • Hmm. when I remove it, then it complains about the next column.  How did I get things out of sync?

    I did drop my entire database, then let it add it back.  After that, does update-database fail?

    below is what got generated

     public override void Up()
            {
                AddColumn("EmailAccounts", "EmailServerTypeId", c => c.Int(nullable: false));
                AddColumn("EmailAccounts", "ServerName", c => c.String(nullable: false));
                AddColumn("EmailAccounts", "Username", c => c.String(nullable: false));
                AddColumn("EmailAccounts", "Password", c => c.String(nullable: false));
                AddColumn("EmailAccounts", "Port", c => c.Int());
                AddColumn("EmailAccounts", "UseSslSocket", c => c.Boolean());
                DropColumn("Users", "NumberEmailsProcessedOnLastRun");
                DropColumn("EmailAccounts", "Pop3ServerName");
                DropColumn("EmailAccounts", "Pop3Username");
                DropColumn("EmailAccounts", "Pop3Password");
                DropColumn("EmailAccounts", "Pop3Port");
                DropColumn("EmailAccounts", "Pop3UseSslSocket");
            }


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Mittwoch, 22. Februar 2012 05:56
  • Hi Peter,

    I think you can check datatable columns to see if "ServerName,EmailServerTypeId" columns have in the datatable.

    Adding -script append  update-database command to see the "T-SQL">>>>update-database -script , in this way, it easy to troubl-shooting.

    If you drop the entire database, you should create database first by Code First, following the blog here:  http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Mittwoch, 22. Februar 2012 06:26
    Moderator
  • Hi Alan,

    I don't see anything in that article that disusses the problem of dropping the database after several migrations.

    That is, When my app starts, it automatically creates a new database with the seed stuff because it sees no database there.  Then, it seems to try and run the migration stuff.  It seems that that creating the database does not somehow let the migrations know that the database is at the current level.

    Please paste here the relevant part of the article so I know what you are talking about.  those are long articles with lots of different concepts.


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Mittwoch, 22. Februar 2012 14:06
  • Hi Peter,

    You should put your seed stuff in Configuration.cs file:

            protected override void Seed(MyContext context)
            {
                //  This method will be called after migrating to the latest version.
                //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
                //  to avoid creating duplicate seed data. E.g.
                //
                    context.EmailAccounts.AddOrUpdate(
                    new EmailAccount{ Id=1, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=2, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=3, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true},
                    new EmailAccount{ Id=4, EmailServerTypeId=1, Password="fa", Port=1, ServerName="test", Username="Alan", UseSslSocket=true}
                    );
            }

    Yes, you should create database first then you can enable-migrations for your application. To run Migration stuff, you should use NuGet mannually.
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Montag, 27. Februar 2012 08:31
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?
    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Freitag, 2. März 2012 08:58
    Moderator
  • I' m in Bellevue this week and have not had a chance to look into this. I will try when I get back home.

    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Freitag, 2. März 2012 14:45