EF 映射表错误“无效的列名 XXX_Id”

Cam*_*gny 3 .net c# entity-framework

我在将我的表映射在一起时遇到问题。我收到错误:

Invalid column name 'Film_Id'.
Run Code Online (Sandbox Code Playgroud)

这是我的实体:

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

}
Run Code Online (Sandbox Code Playgroud)

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

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

上面给出了这个错误:

Invalid object name 'dbo.NormComparableFilms1'.
Run Code Online (Sandbox Code Playgroud)

我想我很接近,但似乎无法做到恰到好处。任何帮助,将不胜感激。

oct*_*ccl 6

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; }
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

And use this configuration:

modelBuilder.Entity<NormComparableFilm>()
            .HasOptional(ncf=>ncf.ComparableFilm)
            .WithMany()
            .HasForeignKey(ncf=>ncf.ComparableFilmId);
Run Code Online (Sandbox Code Playgroud)

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"));
Run Code Online (Sandbox Code Playgroud)