简体   繁体   中英

'Invalid Column Name' Error After Upgrading from Asp.net core 2.0 to 2.1

I've revisited my web site recently and had to upgrade from ASP.net MVC (DBF) core 2.0 to 2.1. Since doing so I'm getting the following error...

SqlException: Invalid column name 'MovieTitleId'. Invalid column name 'MovieTitleId'.

Yet there is no such field 'MovieTitleId' in any part of my code or db.

The error occurs only when the site is accessing the 'many table' Scenes (there is a one-to-many relationship set up in the db with FKs.. Movie > Scenes)

This is the Scene class..

public partial class Scene
    {
        [Key]
        public int SceneId { get; set; }
        [ForeignKey("TitleId")]
        public int? TitleId { get; set; } // foreign key from Movie
        [ForeignKey("LocationSiteId")]
        public int? LocationSiteId { get; set; }
        [ForeignKey("LocationAliasId")]
        public int? LocationAliasId { get; set; }
        public string Notes { get; set; }
        public int? SceneOrder { get; set; }
        public string TitleList { get; set; }
        public LocationAlias LocationAlias { get; set; }
        public LocationSite LocationSite { get; set; }
        public Movie Movie { get; set; }
    } 

And this is the Movie class which on the 'one side' and call Scenes on a typical 'Master/Detail' type web page...

public partial class Movie
    {
        public Movie()
        {
            Scenes = new HashSet<Scene>();
        }

        [Key]
        public int TitleId { get; set; }
        [Required]
        public string Title { get; set; }
        [DisplayName("Title")]
        public string ParsedTitle { get; set; }
        [DisplayName("Year")]
        public int? TitleYear { get; set; }        
        public string ImdbUrl { get; set; }
        public string Summary { get; set; }
        public bool? ExcludeTitle { get; set; }
        public bool? Widescreen { get; set; }


         [DisplayName("Title")]
        public override string ToString() 
        {
            return Title + " (" + TitleYear + ")";
           
        }

       public ICollection<Scene> Scenes { get; set; }
        
    }

The error occurs in the MoviesController.cs...

 Movie movie = _context.Movies.Find(id);
            ViewBag.Scenes = _context.Scenes
                .Where(s => s.TitleId == id)
                .Include(s => s.LocationSite)
                .Include(s => s.LocationSite.LocationPlace)
                .OrderBy(s => s.SceneOrder).ToList(); 

Everything used to work fine until i upgraded to core 2.1. I can't even recall there ever being a field called 'MovietitleId' which is actually 'TitleId'. Is the error msg concatenating the model 'Movie' and column 'TitleId' somehow?

Try adding virtual keyword for your foreign key. Also the ForeignKey Data Annotation should be on that property where you have declared your virtual property just like below. So it should be something like this:

Scene.cs

public int TitleId { get; set; }
[ForeignKey("TitleId")
public virtual Movie Movie { get; set; }

Why virtual?

If you declare your property virtual your virtual property (by default) won't be loaded right away when querying the main object. It will be retrieved from the database ONLY if you try to access it. This is called lazy loading.

If you want to know why to use virtual in detail, you may visit this link: Why are foreign keys in EF Code First marked as virtual?

Hope this helps.

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