EF映射表错误“无效的列名称XXX_Id” [英] EF Mapping Table Error "Invalid column name XXX_Id"

查看:144
本文介绍了EF映射表错误“无效的列名称XXX_Id”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将表格映射到一起时遇到了一个问题。我收到错误:

I am having an issue mapping my tables together. I get the error:

Invalid column name 'Film_Id'.

以下是我的实体:

public class Film
{
    [Key]
    public Int32 Id { get; set; }
    public String Title { get; set; }

    public virtual ICollection<NormComparableFilm> NormComparableFilms { get; set; }
}

public class NormComparableFilm
{
    [Key]
    public Int32 Id { get; set; }
    public Int32 FilmId { get; set; }
    public Int32 ComparableFilmId { get; set; }

    [ForeignKey("FilmId")]
    public virtual Film Film { get; set; }
    [ForeignKey("ComparableFilmId")]
    public virtual Film ComparableFilm { get; set; }

}

OnModelCreating()函数中是否有自定义映射我需要?我尝试添加以下内容,但失败了一个稍微不同的错误:

Is there a custom mapping in the OnModelCreating() function that I need? I tried adding the following but it fails with a slightly different error:

modelBuilder.Entity<Film>()
    .HasMany(f => f.NormComparableFilms)
    .WithMany().Map(t => t.MapLeftKey("FilmId")
    .MapRightKey("ComparableFilmId")
    .ToTable("NormComparableFilms"));

上面提供了这个错误:

Invalid object name 'dbo.NormComparableFilms1'.

我认为我很亲近,但似乎没有得到它正确。任何帮助将不胜感激。

I think I'm close but can't seem to get it just right. Any help would be appreciated.

推荐答案

发生第一个错误是因为您在同一个实体之间创建了两个关系,而Code First约定识别双向关系,但不会在两个实体之间存在多个双向关系时。有多个外键( Film_ID )的原因是Code First无法确定哪个 NormComparableFilm 中的两个属性返回电影链接到 ICollection< NormComparableFilm> 电影类中的code>属性。要解决此代码首先需要一些帮助。您可以使用 InverseProperty 数据注释来指定这些关系的正确结尾,例如:

The first error happened because you are creating two relationships between the same entities and Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.The reason that there are extra foreign keys (Film_ID) is that Code First was unable to determine which of the two properties in NormComparableFilm that return a Film link up to the ICollection<NormComparableFilm> properties in the Film class. To resolve this Code First needs a little of help . You can use InverseProperty data annotation to specify the correct ends of these relationships, for example:

public class NormComparableFilm
{
    [Key]
    public int Id { get; set; }
    public int  FilmId { get; set; }
    public int ComparableFilmId { get; set; }

    [ForeignKey("FilmId")]
    [InverseProperty("NormComparableFilms")]
    public virtual Film Film { get; set; }
    [ForeignKey("ComparableFilmId")]
    public virtual Film ComparableFilm { get; set; }
}

或删除已经使用的数据注释,只添加以下配置:

Or remove the data annotation you already are using and add just these configurations:

modelBuilder.Entity<NormComparableFilm>()
            .HasRequired(ncf=>ncf.Film)
            .WithMany(f=>f.NormComparableFilms)
            .HasForeignKey(ncf=>ncf.FilmId);

modelBuilder.Entity<NormComparableFilm>()
            .HasRequired(ncf=>ncf.ComparableFilm)
            .WithMany()
            .HasForeignKey(ncf=>ncf.ComparableFilmId);

如果在第二个关系中, ComparableFilm 导航属性是可选的,您需要将相应FK的类型更改为可空:

If in the second relationship, the ComparableFilm navigation property is optional, you need to change the type of the corresponding FK as nullable:

public class NormComparableFilm
{
    //...
    public int? ComparableFilmId { get; set; }
}

并使用此配置:

modelBuilder.Entity<NormComparableFilm>()
            .HasOptional(ncf=>ncf.ComparableFilm)
            .WithMany()
            .HasForeignKey(ncf=>ncf.ComparableFilmId);






关于第二个错误,您正在尝试调用电影表作为 NormComparableFilms ,这是EF将按照惯例向由code> NormComparableFilm 实体


About the second error, you are trying to call the Film table as NormComparableFilms that is the default name that EF will give by convention to the table represented by the NormComparableFilm entity.

如果您需要重命名其中一个表,可以使用以下配置:

if you need to rename one of your tables, you can use this configuration:

modelBuilder.Entity<Film>().ToTable("Films"));

这篇关于EF映射表错误“无效的列名称XXX_Id”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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