我有一个课程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
字段更改为“开发人员”。
答案 0 :(得分:0)
好吧,我无法正确解决这个问题,但我发现的唯一解决方法是不将Player
和Developer
分成两个类,而是保留两个类'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;
}
}