简体   繁体   中英

Entity Framework (MVC) - Problem with creating controller for Model that has protected properties

I'm trying to create a MVC 5 controller with views, using Entity Framework. I have a public model class that has protected access modifier on properties which have private set access modifier. Is it possible to create a controller for model that has protected properties with private set?

Model class:

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

Connection string:

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

When I try to create that kind of a controller I get an error: 在此处输入图片说明

I have tried to add [key] prefix before ID property but that didn't help.

Your primary key needs to be public access modifier.

 public class Movie
 {
     public int ID { get; private set; }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM