Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Aug 15 14:39
    dlidstrom commented #1546
  • Aug 11 08:14
    marinusbeck opened #2224
  • Aug 11 06:40
    eyal7773 commented #1940
  • Aug 11 06:33
    marinusbeck opened #2223
  • Aug 11 06:33
    marinusbeck labeled #2223
  • Aug 10 17:45
    k-ahmedhm edited #2222
  • Aug 10 17:45
    k-ahmedhm edited #2222
  • Aug 10 17:43
    k-ahmedhm opened #2222
  • Aug 10 17:43
    k-ahmedhm labeled #2222
  • Aug 09 09:03
    gjvanvuuren labeled #2221
  • Aug 09 09:03
    gjvanvuuren opened #2221
  • Aug 08 17:42
    thomasagarza labeled #2220
  • Aug 08 17:42
    thomasagarza opened #2220
  • Aug 08 08:55
    a44281071 commented #1546
  • Aug 04 11:13
    juwens edited #2219
  • Aug 04 11:03
    juwens edited #2219
  • Aug 04 11:02
    juwens edited #2219
  • Aug 04 10:56
    juwens edited #2219
  • Aug 04 10:55
    juwens edited #2219
  • Aug 04 10:55
    juwens edited #2219
weirdyang
@weirdyang

thanks that works! but now I get a null object reference when I try to fetch the items, it seems my mapping for the DbRef is causing the error:
+ TargetSite {System.Object Deserialize(System.Type, LiteDB.BsonValue)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

            BsonMapper.Global.Entity<LogMessage>()
                .Id(x => x.Id)
                .DbRef(x => x.User, nameof(User));
var results = context.LiteRepository.Fetch<LogMessage>(Query.EQ("Level", (int)LogEventLevel.Error));

Do I need to do an .include?

Leonardo Nascimento
@lbnascimento
@captmomo You shouldn't use DbRef with LiteRepository. LiteRepository is meant to be used as a single "container" for all your data types. If you want distinct collections for different types (that's what DbRef does), you should use LiteCollection<T>
weirdyang
@weirdyang
Oh, so LiteRepository is only to be used for a single collection
Leonardo Nascimento
@lbnascimento
@captmomo Internally it still uses separate collections in the data file, but it's meant to be accessed as a single container.
weirdyang
@weirdyang
ok, sorry I'm a bit confused as to what's the difference. do you mind explaining what you mean by a single container?
isn't dbref for referencing collections within the same db?
Leonardo Nascimento
@lbnascimento
@captmomo The most common way to manipulate data in LiteDB is using LiteCollection<T>. For example, if you have two classes Customer and Product, you would open two distinct collections: var customerCol = db.GetCollection<Customer>(); and var productCol = db.GetCollection<Product>();
@captmomo If you want a single "entry point" for all your data, that's when you use LiteRepository. Internally, it still uses distinct collections, but you can simply throw all your objects at it and it will handle the collections itself.
Leonardo Nascimento
@lbnascimento
@captmomo Now, imagine you have a property of type Product inside Customer. LiteDB will store Product as a sub-document inside Customer, so they'll both be stored in the Customer collection. By using DbRef, you can store instances of Product in its own collection and store only a reference to it in every customer. This, however, should not be used with LiteRepository.
weirdyang
@weirdyang
hmm ok, I'm still not sure why LiteRepository can't handle the the dbref. does it have issues loading the referenced entities?
weirdyang
@weirdyang
image.png
I switched to using collection and it's still throwing the error.
weirdyang
@weirdyang
It seems that I must call .Include, if not the query will fail with the null object reference error. Is this the expected behavior?
Leonardo Nascimento
@lbnascimento
@captmomo Yeah, my explanation wasn't particularly clear, sorry about that. The default behavior of LiteDB is to store sub-objects as embedded documents. DbRef allows you to override this behavior, but it changes the "expected" collection structure. If you want to use LiteRepository, you should let it decide on the correct way to store things.
Leonardo Nascimento
@lbnascimento
@captmomo Regarding the Include, I responded to the issue you opened.
weirdyang
@weirdyang
@lbnascimento Appreciate the help!
i swtiched to just storing the id of the user instead of a ref, everything seems to be working now
weirdyang
@weirdyang

How does the LiteDb IQueryable Skip work with Limit?

If I have 50 entries. and I have a query like:

            UserGenerator userGenerator = new UserGenerator();
            while (count <= 10)
            {
                User newUser = userGenerator.Generate();
                newUser.Name = "test";
                context.LiteRepository.Insert(newUser);
                count++;
                testing.Add(newUser);

            }

            var user = context.LiteRepository.FirstOrDefault<User>(x => x.Name == "test");
            var users = context.LiteRepository.Query<User>().Where(x => x.Name == "test").Skip(1).Limit(5).ToList();

The first query gives me a user with Id = 9, the second query gives me users with ids from 6 to 11.

Is my query syntax incorrect?

weirdyang
@weirdyang
image.png

Newtonsoft.Json.JsonSerializationException: Error getting value from 'AsDateTime' on 'LiteDB.BsonValue'. ---> System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.DateTime'. at lambda_method(Closure , Object )

Getting this issue with this class when serializing:

weirdyang
@weirdyang
mbdavid/LiteDB#169 <<- DateTimeOffSet has builtin support, so I'm not sure what is the issue
Leonardo Nascimento
@lbnascimento
@captmomo Are you using any custom deserializer?
weirdyang
@weirdyang
No.
The JsonConvert is from NewtonSoft, could that be an issue?
Leonardo Nascimento
@lbnascimento
I'm not sure, could you show me how you're using objects of this class? How you're inserting, how they're being serialized/deserialized etc. Where is this error happening?
weirdyang
@weirdyang
   internal async Task<IEnumerable<LogMessage>> ProcessLogEvents(IEnumerable<LogEvent> logEvents, int clientId)
        {
            List<LogMessage> messages = new List<LogMessage>();
            foreach (var evt in logEvents)
            {
                var logMessage = new LogMessage()
                {
                    Level = evt.Level,
                    Message = evt.RenderMessage(),
                    TimeStamp = evt.Timestamp,
                    UserId = clientId
                };
                if (evt.Exception != null)
                {
                    logMessage.Exception = evt.Exception.ToString();
                }

                if (evt.Properties.Any())
                {

                    string complete = await Task.Run(() => Newtonsoft.Json.JsonConvert.SerializeObject(evt.Properties));
                    logMessage.Properties = LiteDB.JsonSerializer.Deserialize(complete).AsDocument;

                }
                messages.Add(logMessage);
            }
            return messages;
        }
Converting the log events into the model for storage
//retrieval

                return _repository.Query<T>()
                .OrderBy(x => x.Id, order: Query.Descending)
                .Where(predicate)
                .Skip(skip)
                .Limit(take)
                .ToList();
            return Ok(results);
the error happens when I return the ActionResult
the last step
Leonardo Nascimento
@lbnascimento
I see you're deserializing LogEvent.Properties with NewtonSoft.Json and serializing it right after with LiteDB.JsonSerializer, there's probably some form of incompatibility (I believe Newtonsoft serializes dates as strings, while LiteDB serializes them to a custom document)
weirdyang
@weirdyang
but there's no datetime in the properteis.
properties*
Leonardo Nascimento
@lbnascimento
What type is it?
weirdyang
@weirdyang
strings
{{"custom":{"TypeTag":null,"Properties":[{"Name":"message","Value":{"Value":"Yesenia-orange"}}]},"SourceContext":{"Value":"Sample.Program"},"Application":{"Value":"Sample"}}}

{{"SourceContext":{"Value":"Sample.Program"},"Application":{"Value":"Sample"}}}
Leonardo Nascimento
@lbnascimento
I believe Newtonsoft is getting lost trying to serialize a BsonValue
weirdyang
@weirdyang
ahah oh no
Leonardo Nascimento
@lbnascimento
Could you try something like: logMessage.Properties = BsonMapper.Global.Serialize(evt.Properties).AsDocument?
weirdyang
@weirdyang
Ok
Leonardo Nascimento
@lbnascimento
Also, if it makes your class easier to use, you can make LogMessage.Properties to be a Dictionary<string,string> and the BsonMapper will serialize it to a document when inserting
weirdyang
@weirdyang

I tried that but it ends up becoming

{{"custom": "{"TypeTag":null,"Properties":[{"Name":"message","Value":{"Value":"Yesenia-orange"}}]},""}

i.e the value of custom is a string, and I cant access the properties.
using a bsondocument, i can do Properties.Custom.TypeTag
if I use dictionary<string, string> i need to do a json(properties.custom) then access the properties
weirdyang
@weirdyang
Newtonsoft.Json.JsonSerializationException: Error getting value from 'AsBoolean' on 'LiteDB.BsonValue'. ---> System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Boolean'. at lambda_method(Closure , Object )
Leonardo Nascimento
@lbnascimento
Are you still calling the NewtonSoft serializer?
weirdyang
@weirdyang
                if (evt.Properties.Any())
                {

                    logMessage.Properties = BsonMapper.Global.Serialize(evt.Properties).AsDocument;

                }
                messages.Add(logMessage);
I'm using a new database too
Leonardo Nascimento
@lbnascimento
In which line is this exception being thrown?
weirdyang
@weirdyang

When the controller action returns the result in

Ok(result)