Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys im battling to to map this using fluent api

this is the movies.cs

C#
[Key]
        [Column]
        public int MovieID { get; set; }
        [Column]
        public string Title { get; set; }
        [Column]
        public string Storyline { get; set; }
        [Column]
        public string Year { get; set; }
        [Column]
        public string Runtime { get; set; }
        [Column]
        public DateTime ReleaseDate { get; set; }
        [Column]
        public DateTime DateTimeAdded { get; set; }
        [Column]
        public DateTime? DateTimeEdited { get; set; }

public virtual ICollection<MovieProducers> MovieProducers { get; set; }


The MovieProducers must be maped like this

C#
public int MovieProducerID { get; set; }
        public int MovieID { get; set; }
        public int PersonID { get; set; }

        public virtual People Person { get; set; }


How would i do this :)
Posted

I'm guessing that you mean EF, using fluent API.

This is a simple model, so shouldn't need a custom mapper.

C#
public class Movie
{    
    [Key]
    public int MovieID { get; set; }
    public string Title { get; set; }
    public string Storyline { get; set; }
    public string Year { get; set; }
    public string Runtime { get; set; }
    public DateTime ReleaseDate { get; set; }
    public DateTime DateTimeAdded { get; set; }
    public DateTime? DateTimeEdited { get; set; }
 
    public virtual ICollection<MovieProducers> MovieProducers { get; set; }
}


Then we link back by simply adding a foreign key attribute to MovieProducers. This attribute points to a virtual property.

C#
public class MovieProducer
{
    [Key]
    public int MovieProducerID { get; set; }

    [ForeignKey("Movie")]
    public int MovieID { get; set; }

    [ForeignKey("Person")]
    public int PersonID { get; set; }
 
    public virtual Movie Movie{ get; set; }
    public virtual People Person { get; set; }
}


You should have fully mapped access with this structure.
 
Share this answer
 
v2
Comments
Nico_Travassos 21-Oct-14 7:25am    
What i was trying to do is some like this but im not sure how to do it with the above example

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<course>().
HasMany(c => c.Students).
WithMany(p => p.CoursesAttending).
Map(
m =>
{
m.MapLeftKey("CourseId");
m.MapRightKey("PersonId");
m.ToTable("PersonCourses");
});
}
Nathan Minier 21-Oct-14 7:27am    
Unless you're already overriding your context's model binder (assuming EF+) you do not need to for these classes. They're simple enough for the default to keep tabs on.
Nico_Travassos 21-Oct-14 7:31am    
MovieProducer is many to many relationship in database so apparently i dont need the class i can just use below example to auto create the table for me is that no correct

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().
HasMany(c => c.Students).
WithMany(p => p.CoursesAttending).
Map(
m =>
{
m.MapLeftKey("CourseId");
m.MapRightKey("PersonId");
m.ToTable("PersonCourses");
});
}
Nathan Minier 21-Oct-14 8:03am    
You'll still need the MoveProducers class. If you want to use the un-attributed model, then you can map it as follows:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Person>()
.HasRequired(c => c.Movie)
.WithMany(d => d.Person)
.Map(m => {
m.MapLeftKey("MovieID");
m.MapRightKey("PersonID");
m.ToTable("MovieProducers");
});

modelBuilder.Entity<movie>()
.HasRequired(c => c.Person)
.WithMany(d => d.Movie)
.Map(m => {
m.MapLeftKey("PersonID");
m.MapRightKey("MovieID");
m.ToTable("MovieProducers");
});
}
Nico_Travassos 21-Oct-14 8:05am    
Check my solution 2 see what i did is correct?
I think i did it correct

Movie.cs

C#
[Key]
        [Column]
        public int MovieID { get; set; }
        [Column]
        public string Title { get; set; }
        [Column]
        public string Storyline { get; set; }
        [Column]
        public string Year { get; set; }
        [Column]
        public string Runtime { get; set; }
        [Column]
        public DateTime ReleaseDate { get; set; }
        [Column]
        public DateTime DateTimeAdded { get; set; }
        [Column]
        public DateTime? DateTimeEdited { get; set; }

        public virtual ICollection<People> MovieProducers { get; set; }


People.cs

C#
public class People
    {
        [Key]
        [Column]
        public int PersonID { get; set; }
        [Column]
        public string FirstName { get; set; }
        [Column]
        public string MiddleName { get; set; }
        [Column]
        public string LastName { get; set; }

        public virtual ICollection<Movies> Movies { get; set; }
    }


DbContext.cs

C#
public DbSet<Movies> Movies { get; set; }
        public DbSet<People> Person { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Movies>().
              HasMany(c => c.MovieProducers).
              WithMany(p => p.Movies).
              Map(
               m =>
               {
                   m.MapLeftKey("MovieID");
                   m.MapRightKey("PersonID");
                   m.ToTable("MovieProducer");
               });
        }
 
Share this answer
 
Comments
Nathan Minier 21-Oct-14 8:18am    
Check my last comment please, your mapping will not work correctly.

HasMan(c=>c.MoveProducers) should be .HasMany(c => c.Person) with this method.
Nico_Travassos 21-Oct-14 8:32am    
It does have many person because if you look at my movies.cs i got --> public virtual ICollection<People> MovieProducers { get; set; }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900