将属性移动到实体框架代码中的新实体首次迁移 [英] Move property to new Entity in Entity Framework Code First Migration

查看:175
本文介绍了将属性移动到实体框架代码中的新实体首次迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用Entity Framework Code First方法。我有一个数据库迁移的问题。



现在我有实体

  public class Event 
{
public int Id {get;组; }
public string标题{get;组; }
public string City {get;组;
}

我现有的数据库中已经有数据。现在我想扩展City属性,像这样

  public class Event 
{
public int Id {得到;组; }
public string标题{get;组; }
public virtual Location Location {get;组;
}

所以,City成为具有许多属性的位置。

  public partial class位置
{
public int Id {get;组; }
public string国家{get;组; }
public string City {get;组; }
public string Address {get;组; }
public string Place {get;组; }
}

所以,我需要在活动表中创建行的位置表并复制城市。并将事件定位的外键设置到位置行。但是我不知道如何使用实体框架迁移来移动现有数据。我已经阅读了许多关于实体框架迁移的文章,但没有发现这种情况。



最好的方法是什么?

解决方案

您可以在3个步骤中执行此操作




  • 添加位置

  • 插入位置

  • Drop City



/ p>

  public class Event 
{
public int Id {get;组; }
public string标题{get;组; }
public string City {get;组;
}




  • 添加Location属性和类

  • 添加位置迁移并更新数据库



    PM>启用迁移



    PM>添加迁移AddLocationTable



    PM>更新数据库


  • 创建用于插入数据的空迁移



    PM>添加迁移InsertLocationData



    该类应该有空的 Up Down 方法。
    Up 方法中添加以下代码。

      (var context = new AppContext())
    {
    var events = context.Set< Event>()。ToArray();
    foreach(事件中的var ev)
    {
    ev.Location = new Location {City = ev.City};
    }
    context.SaveChanges();
    }


  • 再次运行迁移以应用 InsertLocationData 迁移。



    PM>更新数据库


  • 删除 City 属性,并运行迁移以应用更改。



    PM>添加迁移DropCityFromEvent



    PM>更新数据库



I'm using Entity Framework Code First approach in my project. I have a problem with database migration.

Now I have the entity

public class Event
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string City { get; set; }
}

I have already have data in my existing DataBase. Now I want to extend City property, like this

public class Event
{
    public int Id { get; set; }
    public string Title { get; set; }
    public virtual Location Location { get; set; }
}

so, City become to Location with many properties.

public partial class Location
{
    public int Id { get; set; }
    public string Country { get; set; }
    public string City{ get; set; }
    public string Address { get; set; }
    public string Place { get; set; }
}

So, I need for each row in Events Table create row in Locations Table and copy city. And set foreign key for event localtion to location row.But I don't know how to move existing data using Entity Framework Migration. I have read many post on Entity Framework Migration, but didn't find this case.

What is the best way to do this?

解决方案

You could do that in 3 steps

  • Add Location
  • Insert Location
  • Drop City

Starting from

public class Event
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string City { get; set; }
}

  • Add Location property and class
  • Add Location migration and update the database

    PM> Enable-Migrations

    PM> Add-Migration AddLocationTable

    PM> Update-Database

  • Create empty migration for inserting the data

    PM> Add-Migration InsertLocationData

    The class should have empty Up and Down method. Add following code in the Up method.

    using (var context = new AppContext())
    {
        var events = context.Set<Event>().ToArray();
        foreach (var ev in events)
        {
            ev.Location = new Location { City = ev.City };
        }
        context.SaveChanges();
    }
    

  • Run the migration again to apply InsertLocationData migration.

    PM> Update-Database

  • Delete City property and run a migration to apply the changes.

    PM> Add-Migration DropCityFromEvent

    PM> Update-Database

这篇关于将属性移动到实体框架代码中的新实体首次迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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