Add a List<object> to EF

Posted by Billdr on Stack Overflow See other posts from Stack Overflow or by Billdr
Published on 2012-12-15T15:04:06Z Indexed on 2012/12/15 23:03 UTC
Read the original article Hit count: 160

Filed under:
|
|

I'm playing around with EF, trying to get my bearings. Right now I'm writing a blackjack game for a website. The problem is that my whenever I pull a GameState from the database, the playerHand, dealerHand, theDeck, and dealerHidden properties are null.

public class GameState
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int gameSession { get; set; }
    public int playerScore { get; set; }
    public int dealerScore { get; set; }
    public Deck theDeck { get; set; }
    public List<Cards> playerHand { get; set; }
    public List<Cards> dealerHand { get; set; }
    public Cards dealerHidden { get; set; }
    public bool gameOver { get; set; }
}

public class Cards
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int cardId { get; set; }
    public string cardName { get; set; }
    public int cardValue { get; set; }
}

public class GameStateContext : DbContext
{
    public GameStateContext() : base("MyContext")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<GameState>().HasRequired(e => e.theDeck);
        modelBuilder.Entity<GameState>().HasRequired(e => e.dealerHand).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<GameState>().HasRequired(e => e.playerHand).WithMany().WillCascadeOnDelete(false);
        modelBuilder.Entity<GameState>().HasOptional(e => e.dealerHidden);
        modelBuilder.Entity<Deck>().HasRequired(e => e.cards).WithMany().WillCascadeOnDelete(false);
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<GameState> GameStates { get; set; }
    public DbSet<Deck> Decks { get; set; }
    public DbSet<Card> Cards { get; set; }
}

The cards and deck table are populated. Where am I going wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about entity-framework