0

I have tried finding an answer everywhere but I assume I am doing something wrong but can't figure out what.

I have a struct called Genre. Genre has an id and a name.

public struct Genre
{
        public int Id { get; set; }
        public string Name { get; set; }
}

I populate the DB file properly and my .db file looks as follows (comes from a public API):

> db.MoviesGenres.find
[1]: {"_id":12,"Name":"Adventure"}
[2]: {"_id":14,"Name":"Fantasy"}
[3]: {"_id":16,"Name":"Animation"}
[4]: {"_id":18,"Name":"Drama"}
[5]: {"_id":27,"Name":"Horror"}
[6]: {"_id":28,"Name":"Action"}
[7]: {"_id":35,"Name":"Comedy"}
[8]: {"_id":36,"Name":"History"}
[9]: {"_id":37,"Name":"Western"}
[10]: {"_id":53,"Name":"Thriller"}
[11]: {"_id":80,"Name":"Crime"}
[12]: {"_id":99,"Name":"Documentary"}
[13]: {"_id":878,"Name":"Science Fiction"}
[14]: {"_id":9648,"Name":"Mystery"}
[15]: {"_id":10402,"Name":"Music"}
[16]: {"_id":10749,"Name":"Romance"}
[17]: {"_id":10751,"Name":"Family"}
[18]: {"_id":10752,"Name":"War"}
[19]: {"_id":10770,"Name":"TV Movie"}

So far so good, right? Now the issue is when I am trying to get that data again to use elsewhere. I have a method that should be returning the name, but it does not.

public static string GetGenre(int Id)
        {
            LitePlatform.Initialize(new LitePlatformFullDotNet());

            using (var Db = new LiteDatabase(Database.DataFile))
            {
                var Collection = Db.GetCollection<Genre>("MoviesGenres");

                Genre genre = Collection.FindOne(x => x.Id == Id);

                return genre.Name;
            }
        }

When I put a breakpoint, genre has the values id = 0 and name = null. Same happens when I do a static call like Collection.FindOne(x => x.Name == "Animation")

What am I missing?

Community
  • 1
  • 1
Roel
  • 754
  • 3
  • 13
  • 30

1 Answers1

1

Try adding [BsonId] before public int Id { get; set; }. Also, I thought there were query issues with structs. You might want to use a class instead.

Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26