问题
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?
回答1:
are you sure you're looking at the right Db?
it seems so though. Did you get any errors? Any special permissions etc.
My advice is to create a new connection - e.g. config and
<connectionStrings>
<add name="HifiContext" connectionString="Data Source=MACHINE\INSTANCE;Initial Catalog=HiFi;Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>`
...and backup your old just in case. i.e. recreate Db from scratch
If nothing works - try recreating if you just turned migrations on - no other ideas.
As for why the out-of-sync happened - hard to say for sure - but I'm guessing you had 'two connection strings' as well (or at some level).
Make sure that you connection string is 'named' the same as your
dbcontext
- or put connection at your at DbContext directly
. It is sometimes a problem, as it's not apparent what the EF/CF 'makes' as its default.
回答2:
call this method in your startup code to have the database recreated with the new fields:
public void CheckForDBChanes()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<Context>());
}
来源:https://stackoverflow.com/questions/15730662/migration-does-not-alter-my-table