如何使用entityframework在DbModelBuilder中添加thirld列

时间:2017-08-17 04:55:44

标签: c# asp.net-mvc entity-framework model-view-controller ef-code-first

我尝试在代码优先处理的表格中添加新的bool字段,但它不起作用 请有一个我用过的代码。

 modelBuilder.Entity<CollegeActivity>()
            .HasMany<ApplicationRole>(s => s.ApplicationRoles)
            .WithMany(c => c.Activities)
            .Map(cs =>
            {
                cs.MapLeftKey("ActivityId");
                cs.MapRightKey("RoleId");
                cs.MapRightKey("IsInternal");
                cs.ToTable("ActivityRoleRelationship");
            });

表名:ActivityRoleRelationship

和我试图添加的新bool列但未添加

cs.MapRightKey("IsInternal");
你能帮我解决一下我做错了吗?

1 个答案:

答案 0 :(得分:0)

没有办法向表中添加新列。

您需要使用迁移。首先,您需要在bool

上添加ActivityRoleRelationship属性
public class ActivityRoleRelationship
{
    ***

    public bool IsInternal{ set; get; }
}

然后在程序包管理器控制台中键入这些代码

add-migration AddIsInternal

update-database

检查此link以获取有关迁移的更多信息