Search code examples
c#litedb

How to solve LiteDB implementation error?


I'm new to C#. I'm trying to setup LiteDB for Blog in .Net Core 3.1.

namespace Blog.Repos
{
public class BlogRepo
  {
    public LiteDatabase DB { get; set; }
    public LiteCollection<Post> Posts { get; set; }

    public BlogRepo()
    {
        DB = new LiteDatabase(@"Data/Blog.db");
        Posts = DB.GetCollection<Post>("posts"); //Error
    }
  }
}

In VS I'm getting error at Posts = DB.GetCollection("posts"); saying: Error CS0266: Cannot implicitly convert type 'LiteDB.ILiteCollection' to 'LiteDB.LiteCollection'. An explicit conversion exists (are you missing a cast?) (CS0266)

My Model.cs file:

namespace case4ms.Models
{
   public class Model
{
    // Everything needs and ID, not explanation required
    public string ID { get; set; }

    // Will hold the original creation date of the field, 
    // the default value is set to DateTime.Now
    public DateTime Created { get; set; } = DateTime.Now;

    // will hold the last updated date of the field
    ///will initially be set to DateTime.Now, but should be updated on every...update
    public DateTime Updated { get; set; } = DateTime.Now;
    public bool Deleted { get; set; } = false;
}

}

and Post.cs file:

namespace case4ms.Models
{
public class Post:Model
{
    public string Title { get; set; }
    public int Views { get; set; } = 0;
    public string Content { get; set; }
    public string Excerpt { get; set; }
    public string CoverImagePath { get; set; }
    public bool Public { get; set; }
}
}

What am I doing wrong?


Solution

  • Try this:

    public ILiteCollection<Post> Posts { get; set; }