简体   繁体   English

使用实体框架代码优先更改表(添加列)的最便捷方法?

[英]Most convenient way to alter table (add column) using Entity Framework Code First?

I use Code First Entity Framework 6.0 approach and have BaseClass and some of DerivedClass es. 我使用Code First Entity Framework 6.0方法,并具有BaseClass和一些DerivedClass es。 I used to manually update database (all tables derived from BaseClass ). 我曾经手动更新数据库(所有从BaseClass派生的表)。

I want to update (doesn't matter where) data 1 time. 我想1次更新(无论在哪里)数据。

Classes: 类:

public class IItem {

    public int Id {
        get;
        set;
    }

    public string Model {
        get;
        set;
    }

    public int Cost {
        get;
        set;
    }

    //other properties

}


public class MotherBoard : IItem {

    public string Manufacturer {
        get;
        set;
    }

    public string FormFactor {
        get;
        set;
    }

    public string CPUManufacturer {
        get;
        set;
    }

    public string Socket {
        get;
        set;
    }

    public int SocketCount {
        get;
        set;
    }

    //other properties

}

//there are 3 more classes

Ef context: EF上下文:

public class EFDbContext : DbContext {

    public IDbSet<MotherBoard> MotherBoards {
        get;
        set;
    }


    public IDbSet<Processor> Processors {
        get;
        set;
    }


    public IDbSet<VideoCard> VideoCards {
        get;
        set;
    }


    //other

}

Hi could you post your code for your classes and context class? 您好,您可以为自己的课程和上下文课程发布代码吗?

What versions are you using of EF and in what context? 您正在使用什么版本的EF,以及在什么情况下使用?

Do you have just the one context? 您只有一种情况吗? If so try in PowerShell whilst targeting the project with your context 如果是这样,请在PowerShell中尝试,同时根据您的上下文定位项目

enable-migrations 启用迁移
add-migration Initial 添加迁移初始
update-database 更新数据库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码优先迁移 - 实体框架 - 无法向表中添加列 - Code First Migration - Entity Framework - unable to add column to the table 如何在Entity Framework代码优先中将列更改为唯一身份 - How to alter column to unique identity in Entity Framework Code First 使用Entity Framework进行原始查询以添加/更改表的列而不进行迁移 - Raw query to add/alter column of table using Entity Framework without migration 将列添加到 C# 和实体框架代码中的表中,而不删除数据库内容 - Add column to table in C# & Entity Framework code-first without deleting DB content 无法首先使用实体​​框架代码将其他指定的列设置为主键,而不是将ID列设置为表 - unable to set other specified column as primary key instead of Id column to table using entity framework code first 为Entity Framework代码优先应用程序添加索引的正确方法? - The right way to add indexes for Entity Framework code-first applications? 实体框架核心代码首先尝试将存储过程添加为表 - Entity Framework Core Code First trying to add stored procedure as table 有没有办法在运行时使用 Entity Framework Core(代码优先)将旧版本模型实例添加到数据库中? - Is there any way to add old version model instance to database at runtime using Entity Framework Core (code first)? 当使用Entity Framework Code First时,是否可以向根本没有映射到数据库表的类添加属性? - When using Entity Framework Code First, is it acceptable to add properties to a class that are not mapped to a database table at all? 如何在 MVC3 和实体框架上使用 Code First 将另一个表添加到我的数据库中? - How do I add another table to my database using Code First on MVC3 and Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM