locked
Entity Framework Code first questions RRS feed

  • Question

  • I have written these POCOs

    Class Player

    namespace ConsoleApplication5 {
        class Player {
            public int Id { get; set; }
            public string PlayerName { get; set; }
            public virtual ICollection<PlayerSkill> Skill { get; set; }
        }
    }
     
    Class Team 
     
    namespace ConsoleApplication5 {
        class Team {
            public int Id { get; set; }
            public string TeamName { get; set; }
            public Player TeamCaptain { get; set; }
            public virtual ICollection<Player> Players { get; set; }
            public virtual ICollection<Player> ExtraPlayers { getset; }    }
    }
     
    Enum PlayerSkills 

    namespace
    ConsoleApplication5 {
        enum PlayerSkill {
            Batsman,
            Bowler,
            Fielder
        }
    }

    I have written this Context Class

    namespace ConsoleApplication5 {
        class CricketDBContext : DbContext {
            public DbSet<Team> Team { get; set; }
            public DbSet<Player> Player { get; set; }
     
            public CricketDBContext()
                : base("CricketDBConnectionString") {
            }
     
            protected override void OnModelCreating(DbModelBuilder modelBuilder) {
                base.OnModelCreating(modelBuilder);
            }
        }
    }

    And then finally this Seed Method

            protected override void Seed(ConsoleApplication5.CricketDBContext context)
            {
                List<PlayerSkill> list = new List<PlayerSkill>();
                list.Add(PlayerSkill.Batsman);
                var players = Enumerable.Range(1, 11).Select(o => new Player { Id = o, PlayerName = "Player " + o, Skill = list }).ToArray();
                var extras = Enumerable.Range(11, 15).Select(o => new Player { Id = o, PlayerName = "Extra " + o, Skill = list }).ToArray();
                context.Player.AddOrUpdate(player => new { player.Id }, players);
                context.Player.AddOrUpdate(player => new { player.Id }, extras);
                Team[] teams = new Team[1];
                teams[0] = new Team { Id = 1, TeamCaptain = players[0], Players = players, ExtraPlayers = extras, TeamName = " Challengers" };
                context.Team.AddOrUpdate(team => new { team.Id }, teams);
            }
     
     

    Now my questions are

    1. In the database I don't see any table for the Enum. I was hoping that the Enum would be present in the DB. I am not able to add a DbSet<PlayerSkill> to the Context Class because it does not like Enums.

    2. My Seed method is throwing an error

    System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. ---> System.Data.Entity.Core.UpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
       at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.DependencyOrderingError(IEnumerable`1 remainder)
       at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
       at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()

       at Syste


    3. I wonder if I am modelling my POCOs in the right way or not. Basically each team has one player who is the team captain and then 10 players and 2 extras (extra players are like reserve). Each player can have multiple Skills. (batting, bowling, fielding, wicket keeping). So I wonder what is the best way to model this.

    4. When I look at the database what entity framework has created I can see that it has created this table

    But this schema doesn't make any sense... by looking at these tables. how can you tell who is a player and who is extra? and what are the skills of each player?


    Thursday, May 22, 2014 6:02 AM

Answers

  • Hi,

    >> 1. In the database I don't see any table for the Enum. I was hoping that the Enum would be present in the DB. I am not able to add a DbSet<PlayerSkill> to the Context Class because it does not like Enums.

    Currently, Entity Framework does not support to save the enum as a table. If you want it have such an function, you can post your wish to:

    http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions

    And for your case, I would like suggest you defining a table to show skills of a player as:

      

    class  PlayerSkill {
    
            public int PlayerId { get;  set; }
    
            public PlayerSkillEnum PlayerSkill { get; set; }
    
        }

    >> 2. My Seed method is throwing an error

    The error shows “An error occurred while saving entities that do not expose foreign key properties for their relationships.”, and I find that between your Player and Team, there should be a foreign key constraint. So when you want to add a new player, there should be the correspondent team which is already exists in database. For this, you need to expose the foreign key field and assign it when add a new player:

    class  Player {
    
            public int Id { get;  set; }
    
            public string PlayerName { get; set; }
    
            public int TeamID { get; set; }
    
            public virtual ICollection<PlayerSkill> Skill { get; set; }
    
    }

    >> 3. I wonder if I am modelling my POCOs in the right way or not. Basically each team has one player who is the team captain and then 10 players and 2 extras (extra players are like reserve). Each player can have multiple Skills. (batting, bowling, fielding, wicket keeping). So I wonder what is the best way to model this.

    I think you can let 10 players and 2 extras as the same because they are all players and add a flag in player table to show whether the player is an extra player.

    >>When I look at the database what entity framework has created I can see that it has created this table

    You may need to redefine your model as what I mentioned above.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by Fred Bao Friday, May 30, 2014 6:24 AM
    • Marked as answer by Fred Bao Wednesday, June 4, 2014 7:18 AM
    Friday, May 30, 2014 6:24 AM

All replies

  • Hi Technologist,

    Entity will not save Enum for you. It will save the int value of Enum to DB. You can use the following code to build a simple test.

    enum PlayerSkill

        {

            Batsman = 11,

            Bowler = 22,

            Fielder = 33

    }

    Regards,

    Friday, May 23, 2014 7:11 AM
  • Hi,

    >> 1. In the database I don't see any table for the Enum. I was hoping that the Enum would be present in the DB. I am not able to add a DbSet<PlayerSkill> to the Context Class because it does not like Enums.

    Currently, Entity Framework does not support to save the enum as a table. If you want it have such an function, you can post your wish to:

    http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions

    And for your case, I would like suggest you defining a table to show skills of a player as:

      

    class  PlayerSkill {
    
            public int PlayerId { get;  set; }
    
            public PlayerSkillEnum PlayerSkill { get; set; }
    
        }

    >> 2. My Seed method is throwing an error

    The error shows “An error occurred while saving entities that do not expose foreign key properties for their relationships.”, and I find that between your Player and Team, there should be a foreign key constraint. So when you want to add a new player, there should be the correspondent team which is already exists in database. For this, you need to expose the foreign key field and assign it when add a new player:

    class  Player {
    
            public int Id { get;  set; }
    
            public string PlayerName { get; set; }
    
            public int TeamID { get; set; }
    
            public virtual ICollection<PlayerSkill> Skill { get; set; }
    
    }

    >> 3. I wonder if I am modelling my POCOs in the right way or not. Basically each team has one player who is the team captain and then 10 players and 2 extras (extra players are like reserve). Each player can have multiple Skills. (batting, bowling, fielding, wicket keeping). So I wonder what is the best way to model this.

    I think you can let 10 players and 2 extras as the same because they are all players and add a flag in player table to show whether the player is an extra player.

    >>When I look at the database what entity framework has created I can see that it has created this table

    You may need to redefine your model as what I mentioned above.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Edited by Fred Bao Friday, May 30, 2014 6:24 AM
    • Marked as answer by Fred Bao Wednesday, June 4, 2014 7:18 AM
    Friday, May 30, 2014 6:24 AM