实体框架手册数据库更改 [英] Entity Framework manual Database Change

查看:52
本文介绍了实体框架手册数据库更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关Udemy的课程,内容涉及构建.net和mvc5项目.该项目涉及对数据库使用代码优先设计理念.在练习之后,我混合了一些东西,所以我去了辅导老师github代码存储库,并下载了一些课程.在其中一个类中,他在电影类中有一个名为Genre_Id的列名,而我将我的版本命名为GenreId.因此,我愚蠢地进入了电影类,并命名为Genre_Id列,然后转到数据库并将该列手动重命名为Genre_Id.这项手动更改立即开始在我的应用程序中引发异常.

I'm following a a course on Udemy building a .net and mvc5 project. This project involves using the code first design philosophy for databases. I got a couple of things mixed up whilst following the exercises, so I went to the tutors github code repository and downloaded some of the classes. In one of the classes he had a column name called Genre_Id in a Movie class whilst I named my version to GenreId. So I stupidly went into the movie class and called the column Genre_Id and went to the database and renamed the column manually to Genre_Id. This manual change immediately started throwing exceptions in my application.

当我尝试构建我的应用程序时,会抛出系统异常.我做了一些研究,发现了一篇帖子,建议重新初始化迁移可以解决异常.因此,我删除了迁移文件夹,断开了数据库连接,并删除了数据库,以便可以从头开始重新创建迁移和数据库架构.我似乎无法理解的是为什么当我的电影课中不包含该列名时,Visual Studio仍使用Genre_Id1创建迁移,而Genre_Id1是外键.

When I try and build my app, I get thrown system exceptions. I did some research, found a post that suggested reinitialising migrations may resolve the exceptions. So I deleted my migrations folder, disconnected the database connection and deleted the database so that I can recreate the migrations and database schema from scratch. What I don't seem to get is why Visual Studio is still creating migrations with Genre_Id1 which is a foreign key when my class for a movie doesn't contain that column name.

在从头开始构建整个解决方案之前,任何人都可以帮助我.我的异常是内部异常:无效的列名称'Genre_Id1'.我已在整个解决方案中搜索Genre_Id1,检查了我的课程,并确保在更新数据库之前,已删除包含Genre_Id1的add-migration InitialMigration并将其替换为Genre_Id,但是我不确定该在何处定义或选择该列名称,因为构建过程似乎正在寻找它.

Can anyone help me out please before I rebuild the entire solution from scratch. The Exception I have is Inner Exception: Invalid column name 'Genre_Id1'. I've searched for Genre_Id1 in the entire solution, checked my classes, made sure that the add-migration InitialMigration that contained Genre_Id1 was removed and replaced with Genre_Id before I updated the database, but I'm not quite sure where it is defining or picking up this column name up as the build process seems to look for it.

我可以尝试其他任何操作,然后再丢弃项目并重新启动.

Anything else I can try out before I trash the project and restart.

谢谢

推荐答案

您遇到的问题起因于EF(不是您)之前的EF Core选择的默认约定不正确.

The problem you are experiencing originates from poorly chosen default convention by EF (not you) prior EF Core.

如果您的班级具有导航属性,但没有明显的FK属性:

When you have a class with navigation property and no explcit FK property:

public class Movie
{
    // ...
    public Genre Genre { get; set; }
}

隐含的影子FK属性,列名称为 Genre_Id .

the implied shadow FK property and the column name is Genre_Id.

但是显式FK属性名称的默认约定为 GenreId (无下划线).提供时还会更改隐含的列名:

But the default convention for the explicit FK property name is GenreId (no underscore). Which when supplied also changes the implied column name:

public class Movie
{
    // ...
    public int GenreId { get; set; }
    public Genre Genre { get; set; }
}

当您命名属性 Genre_Id

public class Movie
{
    // ...
    public int Genre_Id { get; set; }
    public Genre Genre { get; set; }
}

按照惯例,EF不会将其识别为FK属性,并且将尝试创建默认名称为 Genre_Id 的隐藏(阴影)属性.由于名称是由您的属性保留的,因此EF会附加一个后缀以使其唯一,因此在生成的SQL中会看到 Genre_Id1 .

EF by convention will not recognize it as FK property and will try to create a hidden (shadow) property with the default name Genre_Id. Since the name is preserved by your property, EF appends a suffix in order to make it unique, hence the Genre_Id1 you see in the generated SQL.

有几种方法可以修复它.我个人更喜欢流利的配置,因为它更干净的IMO,但是这是您可以通过数据注释解决的方法:

There are several ways you can fix it. I personally prefer fluent configuration because it's cleaner IMO, but here is how you can resolve it with data annotations:

如果要使用名为 Genre_Id 的FK属性,请使用[ ForeignKey ]属性将其映射为FK:

If you want FK property named Genre_Id, then use [ForeignKey] attribute to map it as FK:

public class Movie
{
    // ...
    [ForeignKey("Genre")]
    public int Genre_Id { get; set; }
    public Genre Genre { get; set; }
}

public class Movie
{
    // ...
    public int Genre_Id { get; set; }
    [ForeignKey("Genre_Id")]
    public Genre Genre { get; set; }
}

如果要使用名为 GenreId 的属性,请使用[ Column ]属性指定列名称:

If you want property called GenreId, then use [Column] attribute to specify the column name:

public class Movie
{
    // ...
    [Column("Genre_Id")]
    public int GenreId { get; set; }
    public Genre Genre { get; set; }
}

我建议您始终创建一个显式迁移,并查看其中包含的内容.如果看到未指定的列,则表明某些不正确的配置导致与EF默认约定冲突.

I would suggest you to always create an explicit migration and see what it contains. If you see columns not specified by you, that's indication of some improper configuration causing conflict with EF default conventions.

这篇关于实体框架手册数据库更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆