迁移不会改变我的表 [英] Migration does not alter my table

查看:65
本文介绍了迁移不会改变我的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在我的项目中启用了迁移,并向 UserProfile 添加了一些字段:

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 添加迁移AddFieldsForUserProfile 并创建:

    ...
    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());
    }
    ...

更新数据库-verbose 产生以下输出:

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.

显然一切顺利,但是在收到错误消息后,认为CreatedOn不存在,我调查了一下服务器浏览器访问数据库,实际上,我的 UserProfile 表中所有4个列都丢失了。我做错了什么?

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?

我发现了我的错误。不知何故,我有两个不同的 mdf 文件 aspnet-Hifi-20130330054424.mdf Hifi.Models .HifiContext.mdf 具有相同的文件大小,我认为两者都是必需的。我的服务器资源管理器正在使用 aspnetxx.mdf ,并且对 HifiContext.mdf 进行了数据库更改。

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.

在相关说明中,我无法正确显示所有注册用户的列表。尽管我可以完美登录,但它始终是空的。以某种方式登录 aspnetxx.mdf 被查询,但是我的 MemberListController 查询了 HifiContext.mdf 。更改连接字符串后,我最初没有注册用户,因此将新用户添加到 HifiContext.mdf 中,该列表正常运行。

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?

推荐答案

您确定要查看正确的Db吗?

似乎是这样。你有什么错误吗?

are you sure you're looking at the right Db?
it seems so though. Did you get any errors? Any special permissions etc.

我的建议是创建一个新的连接-例如config和

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>`

...并备份旧文件以防万一。也就是说,从头开始重新创建Db

...and backup your old just in case. i.e. recreate Db from scratch

如果没有任何效果,请尝试重新创建Db(如果您刚刚启用了迁移),没有其他想法。

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).


确保连接字符串的名称与
dbcontext相同-或直接在DbContext上建立连接

Make sure that you connection string is 'named' the same as your dbcontext - or put connection at your at DbContext directly

。有时会出现问题,因为尚不清楚EF / CF的默认设置是什么。

. It is sometimes a problem, as it's not apparent what the EF/CF 'makes' as its default.

这篇关于迁移不会改变我的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆