Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Extending ApplicationUser and switching existing entries' types

$
0
0

Hello, guys! I got stuck with this problem and I don't know how to solve it:

My application allows players to sign up, so I have an class Player defined as

[Table("Players")]
public class Player : ApplicationUser
{
    [Display(Name = "Birth date")]
    public DateTime? DateBirth { get; set; }
    public virtual ICollection<Playing> Playing { get; set; }
}

I want the players to be able to become Developers, eventually. A Developer would still have all Player' info, like

[Table("Developers")]
public class Developer : Player
{
    public DateTime DateConverted { get; set; }

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

But how do I actually "convert" Players to Developers? I assumed I would only have to instantiate a Developer with the same Id property as the Player 's and save it:

var developer = new Developer
{
    Id = id,
    DateConverted = DateTime.Now
};
Db.Set<TEntity>().Add(entity);
await Db.SaveChangesAsync();
return entity;

However, when I execute await Db.SaveChangesAsync(), I get the following exception:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

I guess it is trying to create a new record in all tables (Developers',Players' andAspNetUsers'), instead of only doing this in Developers.

Does anyone know how to do this properly?

I appreciate all your help! Thank you! :-)


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>