【问题标题】:EF CodeFirst and UserProfile in SimpleMembershipSimpleMembership 中的 EF CodeFirst 和 UserProfile
【发布时间】:2012-10-14 22:55:18
【问题描述】:

我有一个有定义的表格故事

public class Tale
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public DateTime ReleaseDate { get; set; }

    public int enum_TaleAuthorTypeId { get; set; }
    public virtual enum_TaleAuthorType enum_TaleAuthorType { get; set; }

    public int CommentableId { get; set; }
    public virtual Commentable Commentable { get; set; }
}

当我在控制台中键入“update-database”时,我与 CommentableId 的一列和 enum_TaleAuthorTypeId 的一列有很好的关系。

现在,我想添加 UserProfile 并尝试输入如下内容:

    public int UserProfile_UserId { get; set; }
    public virtual UserProfile UserProfile { get; set; }

但是在添加迁移之后,我有这个:

    public override void Up()
    {
        CreateTable(
            "dbo.UserProfile",
            c => new
                {
                    UserId = c.Int(nullable: false, identity: true),
                    UserName = c.String(),
                })
            .PrimaryKey(t => t.UserId);

        AddColumn("dbo.Tales", "UserProfile_UserId", c => c.Int(nullable: false));
        AddColumn("dbo.Tales", "UserProfile_UserId1", c => c.Int());
        AddForeignKey("dbo.Tales", "UserProfile_UserId1", "dbo.UserProfile", "UserId");
        CreateIndex("dbo.Tales", "UserProfile_UserId1");
    }

    public override void Down()
    {
        DropIndex("dbo.Tales", new[] { "UserProfile_UserId1" });
        DropForeignKey("dbo.Tales", "UserProfile_UserId1", "dbo.UserProfile");
        DropColumn("dbo.Tales", "UserProfile_UserId1");
        DropColumn("dbo.Tales", "UserProfile_UserId");
        DropTable("dbo.UserProfile");
    }

起初,EF 似乎想创建表 UserProfile(但它已经创建,我不需要它)。然后,EF 想添加两列 'UserProfile_UserId1' 和 'UserProfile_UserId',但我确实需要一列作为值。

我的问题是什么?

【问题讨论】:

  • 你用流畅的句法映射关系了吗?
  • 不知道什么是流畅的语法,但我只有内置的 AccountModels 和其他一些类,比如 Tale。
  • 好吧,如果您不打算使用常规映射,那么您需要显式映射。这是通过属性或流利的映射完成的。解决此问题的最简单方法是删除 `UserProfile_UserId 属性。但是您还需要向 UserProfile 添加一个 Tales 导航属性。
  • 但是我将如何初始化用户的价值,谁添加了故事?我认为它应该在 Tale 类中的 UserProfile_UserId 属性中
  • 我不明白,为什么前两个表添加完美,而 UserProfile 没有 =\

标签: .net asp.net-mvc entity-framework


【解决方案1】:

尝试在您的上下文中添加

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Ignore<UserProfile>();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    相关资源
    最近更新 更多