简体   繁体   中英

EntityFramework : Mapping existing one-to-many relationship to Model

I am using the Linq-to-Sqlite ORM using the Entity Framework. Since Linq-to-Sqlite cannot itself create tables based on a model using code first approach, I am forced to reuse my existing table schema to build the entity model design. Below is my expected model design:

public class Movie
{
    public int MovieId { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Genre> Genres { get; set; }

}

public class Genre
{
    public int GenreId { get; set; }
    public string GenreName { get; set; }
}

Below is existing table schema:

CREATE TABLE Movie
(
    MovieId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    Title TEXT
)

CREATE TABLE "Genre"
(
    'GenreId' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    'GenreName' TEXT NOT NULL,
     UNIQUE(GenreName)
)

CREATE TABLE MovieGenre
(
    'MovieId' INTEGER NOT NULL REFERENCES Movie(MovieId),
    'GenreId' INTEGER NOT NULL REFERENCES Genre(GenreId),
     UNIQUE(MovieID, GenreID)
)

So, basically a one movie can have many Genre's assigned to it. However, I would want to have any relationship from the Genre entity to the Movie entity.

Below is my overridden OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        try
        {
            modelBuilder.Entity<Movie>()
                .HasMany<Genre>(m => m.Genres)
                .WithOptional()
                .Map(mg =>
                    {
                        mg.ToTable("MovieGenre");
                        mg.MapKey("GenreId");
                    });
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I am getting an exception Message=The specified table 'MovieGenre' was not found in the model. Ensure that the table name has been correctly specified.

How can we map the relationship table MovieGenre to the Movie and Genre entity so that I can populate the Movie.Genres ICollection property ?

That kind of relationship is many-to-many and not one-to-many . I'm not an expert in SQL Lite but I think the last table should be like this (see this link ):

CREATE TABLE "MovieGenres"
(
  'MovieId' INTEGER NOT NULL,
  'GenreId' INTEGER NOT NULL,
   PRIMARY KEY (MovieId, GenreID),

  FOREIGN KEY (MovieId) REFERENCES Movies(MovieId),
  FOREIGN KEY (GenreId) REFERENCES Genres(GenreId)
)

To map that relationship in EF, the configuration would be this way:

modelBuilder.Entity<Movie>().HasMany(m => m.Genres)
                             .WithMany(g =>g.Movies)
                             .Map(c =>{  c.MapLeftKey("MovieId");
                                         c.MapRightKey("GenreId");
                                         c.ToTable("MovieGenres");
                                      });

But first add the Movies collection property in the Genre entity:

public class Genre
{
 //...
 public virtual ICollection<Movie> Movies{ get; set; }
}

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