将应用程序用户Player转换为开发人员扩展Player的用户Developer

时间:2014-10-12 19:19:54

标签: asp.net-mvc-5 entity-framework-6 identity

我有一个课程Player - 延伸ApplicationUser - 和Developer - 延伸Player - 。我希望用户能够随时成为开发人员,所以我必须以某种方式将某个类Player的对象转换为某个类Developer,但我很困惑关于如何实际做到这一点。

public class Player : ApplicationUser
{
    public virtual ICollection<Playing> Playing { get; set; }
}

public class Developer : Player
{
    public DateTime DateConverted { get; set; }

    public virtual ICollection<Game> Games { get; set; }
}

我虽然可以检索对象Player,但是将其从数据库中删除,实例化Developer类型的对象,复制所有信息并保存。它会是这样的:

public virtual async Task<Developer> Convert(Player player)
{
    var developer = new Developer
    {
        Id             = player.Id,
        DateBirth      = player.DateBirth,
        DateCreated    = player.DateCreated,
        DateUpdated    = player.DateUpdated,
        Email          = player.Email,
        EmailConfirmed = player.EmailConfirmed,
        UserName       = player.UserName
    };

    Db.Players.Remove(player);
    Db.Developers.Add(developer);
}

问题是:当我删除播放器的条目时,所有关系都会丢失。

有更好/更简单的方法吗?据我所知,我只需要将discriminator表中的ApplicationUser字段更改为“开发人员”。

1 个答案:

答案 0 :(得分:0)

好吧,我无法正确解决这个问题,但我发现的唯一解决方法是不将PlayerDeveloper分成两个类,而是保留两个类'Player中的属性。差异仅由属性DateCoverted给出,该属性表示给定Player转换为Developer的日期。

这样的事情:

public class Player : ApplicationUser
{
    public virtual ICollection<Playing> Playing { get; set; }

    public DateTime? DateConverted { get; set; }

    public virtual ICollection<Game> Games { get; set; }
}

public class PlayerService : Service<Player>
{
    public virtual async Task<Player> Convert(string id)
    {
        var developer = await Db.Players.FindAsync(id);
        developer.DateConverted = DateTime.Now;
        await Update(developer);
        return developer;
    }
}