Как решить ошибку реализации LiteDB? - PullRequest
0 голосов
/ 26 апреля 2020

Я новичок в C#. Я пытаюсь настроить LiteDB для блога в. 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
    }
  }
}

В VS я получаю сообщение об ошибке в Posts = DB.GetCollection ("posts"); поговорка: Ошибка CS0266: невозможно неявное преобразование типа «LiteDB.ILiteCollection» в «LiteDB.LiteCollection». Существует явное преобразование (вам не хватает приведения?) (CS0266)

Файл My Model.cs:

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;
}

}

и файл Post.cs:

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; }
}
}

Что я делаю не так?

1 Ответ

1 голос
/ 26 апреля 2020

Попробуйте это:

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