核心 MVC 实体框架存储库模式

时间:2021-01-25 18:37:33

标签: asp.net-core model-view-controller design-patterns repository core

请帮帮我。 我是新的 .net core mvc 和这里。 我正在开发一个非常简单的博客网站。但我收到一个错误。 课后

    
using System.Collections.Generic;

namespace DataAccess
{
     public class Post
    {
        public int PostId { get; set; }
        public string PostTitle { get; set; }
        public string PostWriter { get; set; }
        public string PostDate { get; set; }
        public string PostİmageUrl { get; set; }
        public string PostContent { get; set; }
        public List<Post> Posts { get; set; }


    }


}
using System.Collections.Generic;
using System.Linq;
using DataAccess.Abstract;
using Microsoft.EntityFrameworkCore;
using static DataAccess.Entity;

namespace DataAccess.Concrete.SqLite
{
    public class PostRepository : IPostRepository
    {
        private Context db = new Context ();
        public List<Post> GetAll()
        {
            return db.Posts.ToList();
        }

        public Post GetById(int id)
        {
            return db.Posts.Find(id);
        }
    }
}

家庭控制器

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Blog.Models;
using DataAccess.Abstract;
using DataAccess;

namespace Blog.Controllers
{
    public class HomeController : Controller
    {
       
        private IPostRepository _postRepository;
        private ICommentRepository _commentRepository;
        private ICategoryRepository _categoryRepository;

        public HomeController(IPostRepository postRepository, ICommentRepository commentRepository, ICategoryRepository categoryRepository)
        {
            this._postRepository = postRepository;
            this._commentRepository = commentRepository;
            this._categoryRepository = categoryRepository;
        }
        public IActionResult Index()
        {
            var Post = new Post ()
            {
                Posts = _postRepository.GetAll()
            };
            return View(Post);
            // return View(new PostViewModel()
            // {
            //     Posts = _postRepository.GetAll()
            // });
            
        }

        public IActionResult Privacy()
        {
            return View();
        }
    }
}

我的观点

@model Post

@{

  var P = Model.Posts;
}
      <!-- Post Content Column -->
      <div class="col-lg-8">

        <!-- Title -->
        @foreach (var item in P)
        {
                 <h1 class="mt-4">@item.PostTitle</h1>
   
        }
        </div>

这是我的代码。 [我的数据库][1] [我的错误][2] [1]:https://i.stack.imgur.com/IOc10.png [2]:https://i.stack.imgur.com/5ADC2.png 你觉得我能怎么解决这个问题?

0 个答案:

没有答案
相关问题