实体框架(MVC) - 为具有受保护属性的Model创建控制器的问题

问题描述 投票:2回答:1

我正在尝试使用Entity Framework创建一个带有视图的MVC 5控制器。我有一个公共模型类,它在具有私有集访问修饰符的属性上具有受保护的访问修饰符。是否可以为具有私有集的受保护属性的模型创建控制器?

型号类:

public class Movie
{
    protected int ID { get; private set; }
    protected string Title { get; private set; }
    protected DateTime ReleaseDate { get; private set; }
    protected string Genre { get; private set; }
    protected decimal Price { get; private set; }
}

连接字符串:

<add name="MovieDBContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf" providerName="System.Data.SqlClient" />

当我尝试创建那种控制器时,我得到一个错误:enter image description here

我试图在ID属性之前添加[key]前缀,但这没有帮助。

c# asp.net-mvc entity-framework
1个回答
0
投票

您的主键需要是public访问修饰符。

 public class Movie
 {
     public int ID { get; private set; }
 }
© www.soinside.com 2019 - 2024. All rights reserved.