1
Reply

Need help in querying many to many relationship.

Satish Bhat

Satish Bhat

Jun 24 2011 3:18 AM
2.1k
Hi,

Need help in querying many to many relationship.

I have 3 tables
1] Games
GameIDGame Title
1FIFA 10
2Halo 3
3DIRT

2] Platforms
PlatformIDPlatformName
1XBox 360
2PS3
3PSP
4Wii

3] GamePlatforms

GameIDPlatformID
11
12
13
22
23

Now i need to query All Games in PlatformID
X

POCO

public class Game
    {
        [Key]
        public int GameId { get; set; }


        [Column("GameTitle")]
        public string Title { get; set; }
 
 public virtual ICollection<Platform> Platforms { get; set; }
    }   

 public class Platform
    {
        [Key]
        public int PlatformId { get; set; }

        [Column("Platform")]
        public string Name { get; set; }

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

// Mapping

modelBuilder.Entity<Game>()
            .HasMany(u => u.Platforms)
            .WithMany()
            .Map(a =>
            {
                a.ToTable("GamePlatforms");
                a.MapLeftKey("GameId");
                a.MapRightKey("PlatformId");
            });

Service Layer

 public IEnumerable<MemberGame> Search(int platformId, string gameName)
        {
            Platform _platform = platformRepository.Get(platformId);
            var predicate = PredicateBuilder.True<MemberGame>();
            predicate = predicate.And(mg => mg.Game.Platforms.Contains(_platform)).And(mg => mg.Game.Title == gameName).Or(mg => mg.Game.Title.Contains(gameName));


            return repository.GetMany(predicate);          
        }

Data Layer

public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
        {
            return _dbSet.Where(where).ToList();
        }  


throws an error Unable to create a constant value of type 'XXXX.BO.Platform'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Please help me to solve the problem.

Thanks inadvance
Satish


Answers (1)