I just enabled migrations in my project and added a few fields to UserProfile
:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Description { get; set;}
public DateTime? CreatedOn { get; set; }
public DateTime? LastAccess { get; set; }
}
I Add-migration AddFieldsForUserProfile
and it created:
...
public override void Up()
{
AddColumn("dbo.UserProfile", "Email", c => c.String());
AddColumn("dbo.UserProfile", "Description", c => c.String());
AddColumn("dbo.UserProfile", "CreatedOn", c => c.DateTime());
AddColumn("dbo.UserProfile", "LastAccess", c => c.DateTime());
}
...
Update-database -verbose
yielded this output:
Target database is: 'Hifi.Models.HifiContext' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Convention).
Applying code-based migrations: [201303311011083_AddFieldsForUserProfile].
Applying code-based migration: 201303311011083_AddFieldsForUserProfile.
ALTER TABLE [dbo].[UserProfile] ADD [Email] [nvarchar](max)
ALTER TABLE [dbo].[UserProfile] ADD [Description] [nvarchar](max)
ALTER TABLE [dbo].[UserProfile] ADD [CreatedOn] [datetime]
ALTER TABLE [dbo].[UserProfile] ADD [LastAccess] [datetime]
[Inserting migration history record]
Running Seed method.
Apparently all went well, but after recieving an error that the coloumn CreatedOn does not exist, I looked into the database with the Server Explorer and indeed, all 4 coloumns are missing in my UserProfile
table. What did I do wrong?
Edit
I found my error. Somehow I had two different mdf
files aspnet-Hifi-20130330054424.mdf
and Hifi.Models.HifiContext.mdf
which had the same file size and I assumed both were necessary. My Server Explorer was using the aspnetxx.mdf
and the database changes were made to HifiContext.mdf
. Shame on me.
On a related note I had trouble correctly displaying a list of all registered users. It was always empty altough I could login flawlessly. Somehow for login aspnetxx.mdf
was queried but my MemberListController
queried HifiContext.mdf
. After changing my connection string I had initially no registered users, new ones were added to HifiContext.mdf
and the list worked properly. How did this happen?