C#实体框架:使用自动迁移,重命名表字段而不会丢失数据 [英] C# Entity Framework : Rename table field without losing data, using automatic migrations

查看:100
本文介绍了C#实体框架:使用自动迁移,重命名表字段而不会丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework的自动迁移和代码优先方法。
我不使用数据包管理器控制台,也无法访问编码的迁移。

I am using Entity Framework's automatic migrations with a code-first approach. I dont use the packet manager console and dont have access to coded migrations.

我在模型中的一行代表我的表:

I have a line representing my table in my model here :

        public virtual DbSet<Customer> Customers { get; set; }

我重命名了Customer字段之一:

I renamed one of the fields of Customer :

[Table("Customers")]
public partial class Customer
{
    [Key]
    [Column(TypeName = "numeric")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int idCustomer { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string mail { get; set; }
    public string password { get; set; }
}

我刚刚将 mail更改为 mailModified。
但是,当数据库更新时,实体从名称字段中删除了所有数据。
我认为它删除了我的列,以使用新名称创建一个新列。

I just changed "mail" to "mailModified". But when the database got updated, Entity deleted all data from the name field. I think it deleted my column to create a new one with the new name.

如何避免这种情况?

感谢您的参与

推荐答案

在创建表类时尝试使用列属性

Try using Column Attribute when you create your table class

class Table
{
    [Column("ColumnName")]
    public int Column1 { get; set; }
    [Column("ColumnName")]
    public int Column2 { get; set; }
}

这篇关于C#实体框架:使用自动迁移,重命名表字段而不会丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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