简体   繁体   中英

EF Mapping Table Error “Invalid column name XXX_Id”

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

Invalid column name 'Film_Id'.

Here are my Entities:

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

}

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"));

The above gives this error:

Invalid object name 'dbo.NormComparableFilms1'.

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

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

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

And use this configuration:

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

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"));

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