我正在尝试使用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" />
我试图在ID属性之前添加[key]
前缀,但这没有帮助。
您的主键需要是public
访问修饰符。
public class Movie
{
public int ID { get; private set; }
}