[英]'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.我最近重新访问了我的 web 站点,不得不从 ASP.net MVC (DBF) 核心 2.0 升级到 2.1。 Since doing so I'm getting the following error...
由于这样做,我收到以下错误...
SqlException: Invalid column name 'MovieTitleId'. SqlException:列名“MovieTitleId”无效。 Invalid column name 'MovieTitleId'.
列名“MovieTitleId”无效。
Yet there is no such field 'MovieTitleId' in any part of my code or db.然而,在我的代码或数据库的任何部分都没有这样的字段“MovieTitleId”。
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)该错误仅在站点访问“多表”场景时发生(在数据库中与 FK 建立了一对多关系。电影 > 场景)
This is the Scene class..这是场景 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...这是电影 class ,它在“一侧”并在典型的“主/细节”类型 web 页面上调用场景......
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...错误发生在 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.在我升级到核心 2.1 之前,一切正常。 I can't even recall there ever being a field called 'MovietitleId' which is actually 'TitleId'.
我什至不记得曾经有一个名为“MovietitleId”的字段实际上是“TitleId”。 Is the error msg concatenating the model 'Movie' and column 'TitleId' somehow?
错误消息是否以某种方式连接了 model 'Movie' 和列 'TitleId' ?
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.
此外,ForeignKey Data Annotation 应该在您声明虚拟属性的那个属性上,如下所示。 So it should be something like this:
所以它应该是这样的:
Scene.cs场景.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.如果您将您的属性声明为虚拟,则在查询主 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?如果您想详细了解为什么要使用virtual ,可以访问此链接: 为什么 EF Code First 中的外键标记为 virtual?
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.