如何将字符串键改为int [英] How to change string key to int

查看:255
本文介绍了如何将字符串键改为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#EntityFramework和CodeFirst。我也使用迁移(自动和手动迁移)。



我有一个模型 -

  public class MyClass 
{
public string Key {set;得到; }
public string Something {set;得到;
}

而且我已经有一个数据库与行。



现在我想更改模型:



a。将密钥列更改为名称(这不是迁移问题,特别是自动迁移)



b。 添加一个新的属性 - 将会是身份(1,2,3 ...)的intKey。



这样做而不删除现有行? (我想让他们得到一个自动的id)

解决方案

我决定测试一下。我从这个类开始,添加了迁移,更新了数据库,并插入了一些数据:

  public class MyClass 
{
[Key] //添加它以摆脱错误:
// EntityType'Class1'没有定义键。定义此EntityType的键。
public string Key {set;得到; }
public string Something {set;得到; }
}

然后我将其更改为此并运行了第二次迁移。

  public class MyClass 
{
public int ID {get;组; }
public string Key {set;得到; }
public string Something {set;得到; }
}

这是生成的迁移。默认情况下,EF将创建一个名称为ID的整数列,其中标识为true:

  public override void Up 
{
DropPrimaryKey(dbo.MyClasses);
AddColumn(dbo.MyClasses,ID,c => c.Int(nullable:false,identity:true));
AlterColumn(dbo.MyClasses,Key,c => c.String());
AddPrimaryKey(dbo.MyClasses,ID);
}

然后我更改了名称并运行另一个迁移。但是为了避免丢失数据,我在列删除之前添加了一些sql:

  public override void Up()
{
AddColumn(dbo.MyClasses,Name,c => c.String());
Sql(UPDATE dbo.MyClasses SET Name = [Key]); //手动添加到迁移
DropColumn(dbo.MyClasses,Key);
}

public override void Down()
{
AddColumn(dbo.MyClasses,Key,c => c.String()) ;
Sql(UPDATE dbo.MyClasses SET [Key] = Name); //手动添加到迁移
DropColumn(dbo.MyClasses,Name);
}


I use C# EntityFramework and CodeFirst. Also I use migrations (automatic & manual migrations).

I have a model -

public class MyClass
{
    public string Key { set; get; }
    public string Something { set; get; } 
}

And I already have a database with rows.

Now I want to change the model :

a. Change "Key" column to "Name" (which is not a problem with migration, especially automatic migrations)

b. Add a new property - an int "Key" that will be identity (1, 2, 3...).

How can I do that without deleting the existing rows? (I want them to get an automatic id)

解决方案

I decided to test this out. I started with this class, added a migration, updated the database, and inserted some data:

public class MyClass
{
    [Key]//Added this to get rid of the error: 
         //EntityType 'Class1' has no key defined. Define the key for this EntityType.
    public string Key { set; get; }
    public string Something { set; get; } 
}

Then I changed it to this and ran a second migration.

public class MyClass
{
    public int ID { get; set; }
    public string Key { set; get; }
    public string Something { set; get; } 
}

Here is the generated migration. By default, EF will make an integer column named "ID" the key - with identity true:

public override void Up()
{
    DropPrimaryKey("dbo.MyClasses");
    AddColumn("dbo.MyClasses", "ID", c => c.Int(nullable: false, identity: true));
    AlterColumn("dbo.MyClasses", "Key", c => c.String());
    AddPrimaryKey("dbo.MyClasses", "ID");
}

Then I changed Key to Name and ran another migration. But to avoid losing data, I added some sql before the column is dropped:

public override void Up()
{
    AddColumn("dbo.MyClasses", "Name", c => c.String());
    Sql("UPDATE dbo.MyClasses SET Name = [Key]"); //Manually added to migration
    DropColumn("dbo.MyClasses", "Key");
}

public override void Down()
{
    AddColumn("dbo.MyClasses", "Key", c => c.String());
    Sql("UPDATE dbo.MyClasses SET [Key] = Name"); //Manually added to migration
    DropColumn("dbo.MyClasses", "Name");
}

这篇关于如何将字符串键改为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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