none
Entity Framework and migration issue RRS feed

  • Question

  • this is my sample class

    public class Contacts
    {
        [Key]
        public int ContactID { get; set; }
    
        public string Phone { get; set; }
        public string Fax { get; set; }
        public bool IsDefault { get; set; }
    
        public int AddressID { get; set; }
        public virtual Addresses Customer { get; set; } 
    
    }

    in our database EF already create table for Contacts. i add one new field to contact class and that is public bool IsDefault { get; set; }

    when i try to migrate like this way

        Enable-Migrations
        Add-Migration AddIsDefault
        Update-Database -Verbose

    then VS show new fields is added. the sql i got for that from package manager console like

    ALTER TABLE [dbo].[Contacts] ADD [IsDefault] [bit] NOT NULL DEFAULT 0
    ALTER TABLE [dbo].[Customers] ADD [Address1] [nvarchar](max)
    ALTER TABLE [dbo].[Customers] ADD [Address2] [nvarchar](max)
    ALTER TABLE [dbo].[Customers] ADD [Phone] [nvarchar](max)
    ALTER TABLE [dbo].[Customers] ADD [Fax] [nvarchar](max)
    INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model],  [ProductVersion])
    VALUES (N'201609071306198_AddIsDefault', N'EFTest.TestDBContext',

    i though new fields has been added but when i query contacts table then saw no new field has been added.

    migration details

    namespace EFTest.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
    
        public partial class AddIsDefault : DbMigration
        {
            public override void Up()
            {
                AddColumn("dbo.Contacts", "IsDefault", c => c.Boolean(nullable: false));
                AddColumn("dbo.Customers", "Address1", c => c.String());
                AddColumn("dbo.Customers", "Address2", c => c.String());
                AddColumn("dbo.Customers", "Phone", c => c.String());
                AddColumn("dbo.Customers", "Fax", c => c.String());
            }
    
            public override void Down()
            {
                DropColumn("dbo.Customers", "Fax");
                DropColumn("dbo.Customers", "Phone");
                DropColumn("dbo.Customers", "Address2");
                DropColumn("dbo.Customers", "Address1");
                DropColumn("dbo.Contacts", "IsDefault");
            }
        }
    }

    i did not write any code for seed. is it the reason for which new field has not been added to my contacts table ?

    please guide me why new field called IsDefault not being added to table event after migration command issuing ? thanks


    • Edited by Sudip_inn Wednesday, September 7, 2016 2:42 PM
    Wednesday, September 7, 2016 2:42 PM

Answers

  • before connection string is in app.config file

      <connectionStrings>
        <add name="TestDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\DB\TestDB.mdf;Integrated Security=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

    Issue solved

    problem was at my end.

    my connection string was looking like

    EF migration do not understand the meaning of DataDirectory path

    the moment i hard code the db file path like below connection then issue was solved.

    <connectionStrings> <add name="TestDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=c:\users\user1\documents\visual studio 2013\Projects\EFTest\EFTest\DB\TestDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

    |DataDirectory| does not work in console apps. i paste full db path to AttachDbFilename

    Thursday, September 8, 2016 11:06 AM

All replies

  • Are you sure you're looking at the correct database? 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Wednesday, September 7, 2016 2:58 PM
  • issue has been solved. error was there at my end.
    Wednesday, September 7, 2016 3:15 PM
  • Hi Mou_inn,

    I am glad to know you solved this problem. Could you please share your solution.?

    It will be very beneficial for other community members who have the similar questions. 

    Thank you.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 8, 2016 2:20 AM
    Moderator
  • before connection string is in app.config file

      <connectionStrings>
        <add name="TestDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\DB\TestDB.mdf;Integrated Security=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

    Issue solved

    problem was at my end.

    my connection string was looking like

    EF migration do not understand the meaning of DataDirectory path

    the moment i hard code the db file path like below connection then issue was solved.

    <connectionStrings> <add name="TestDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=c:\users\user1\documents\visual studio 2013\Projects\EFTest\EFTest\DB\TestDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

    |DataDirectory| does not work in console apps. i paste full db path to AttachDbFilename

    Thursday, September 8, 2016 11:06 AM