Evg*_*kiy 2 c# asp.net-mvc datamodel ado.net-entity-data-model
我是C#的大三学生,无法使用搜索找到解决方案
我有一个数据库模型(EDM)
我在models文件夹中创建了一个类文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace photostorage.Models
{
public class PhotosRepository
{
private fotostorageEntities db = new fotostorageEntities();
public IEnumerable<photos> FindUserPhotos(string userid)
{
return from m in db.photos
select m;
}
public photos GetPhotosById(int photoid)
{
return db.photos.SingleOrDefault(d => d.id == photoid);
}
}
}
Run Code Online (Sandbox Code Playgroud)
接下来一个为该模型创建一个控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using photostorage.Models;
namespace photostorage.Controllers
{
public class PhotosController : Controller
{
//
// GET: /Photos/
public ActionResult ViewPhoto(string userid, int photoid)
{
photos CurrentPhoto = PhotosRepository.GetPhotosById(photoid);
if (CurrentPhoto == null)
return View("NotFound");
else
return View("ViewPhoto", CurrentPhoto);
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果我有一个错误:非静态字段,方法或属性phototorage.Models.PhotosRepository.GetPhotosById(int)需要对象引用。
数据库中的表名称-照片EDM连接字符串名称-fotostorageEntities
需要帮助,因为我真的不知道解决方案。
您当前正在GetPhotosById以静态方法进行调用。您需要创建的实例PhotosRepository。
public ActionResult ViewPhoto(string userid, int photoid)
{
PhotosRepository photosRepository = new PhotosRepository();
photos CurrentPhoto = photosRepository.GetPhotosById(photoid);
if (CurrentPhoto == null)
return View("NotFound");
else
return View("ViewPhoto", CurrentPhoto);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7101 次 |
| 最近记录: |