locked
How to manually define DBContext code snippet for table without primary keys? RRS feed

  • Question

  • User973797352 posted

    Hello,

    I followed these instructions ( https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db ) to automatically create database models/entity classes and the DBContext class, however one of the tables does not have any primary keys defined so the Scaffold-DbContext command did not generate the entity class for it. 

    This table is basically a small 2 column table for defining a many-to-many relationship between 2 other tables.

    Creating the Entity class was straight forward, but what should the proper code snippet be for the DBContext class for this?  

    The manually created Entity class:

    public class UserLocationManyToMany
    {
    
    
        public int UserId { get; set; }
    
        public int? LocationId { get; set; }
    
    
    }

    The following is the DBContext code snippet (pls correct if incorrect):

    modelBuilder.Entity<UserLocationManyToManyTable>(entity =>
    {
    
    entity.ToTable("UserLocation");
    entity.HasIndex(e => e.UserId).HasName("userId_idx");
    
    entity.Property(e => e.UserId).HasColumnName("userId");
    entity.Property(e => e.LocationId).HasColumnName("locationId");
    
    }

    In addition to the statements above, does anyone know how to define the following?:

    1. the many-to-many relationship (between "User" table to "Location" table)
    2. the primary key (my understanding is that this is necessary)

    Thanks!

    victor

    Wednesday, March 15, 2017 6:47 PM

All replies

  • User-832373396 posted

    Hi Victor168,

    I suggest that you could use code like [key] to mark UserId.

    And here is the article on how to set the many-to-many relationship:

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx 

    Best regards,

    Jolie

    Thursday, March 16, 2017 3:07 AM
  • User973797352 posted

    Hi Jolie!

    Thanks for your help!  I tried to follow the solution in the link you provided to define a many-to-many relationship similar to the following:

    modelBuilder.Entity<Student>()
    .HasMany<Course>(s => s.Courses)
    .WithMany(c => c.Students)
    .Map(cs =>
    	{
    	cs.MapLeftKey("StudentRefId");
    	cs.MapRightKey("CourseRefId");
    	cs.ToTable("StudentCourse");
    });

    But I kept getting errors.  Using the above example code, when I try that on my own code, the "s" is of type Student, and "s.Courses" is not possible since Courses is a table (a separate Entity class).  In other words, my Visual Studio IDE is telling me that "s.Courses" does not exist because "s" is the Student class and it does not contain a property called "Courses".

    Semantically, I understand what the above code is trying to do.  Yet, syntactically, how is it possible?  My IDE is saying the code is wrong and therefore cannot compile.

    Thursday, March 16, 2017 2:24 PM
  • User-832373396 posted

    Hi Victor168 ,

    Sorry for my later . May I know your database design whether exist 3 table ,just like Student ,StudentCourses ,Courses ,so that we could apply this many to many relationship .

    public class Course { public Course()
        {
            this.Students = new HashSet<Student>(); // it is necessary to new HashSet
        }
    
        public int CourseId { get; set; }
        public string CourseName { get; set; }
    
        public virtual ICollection<Student> Students { get; set; } //and did you add this on your machine ?
    }

    For this code :

    modelBuilder.Entity<Student>()
                    .HasMany<Course>(s => s.Courses)    //it means that the student model have many Courses various (ICollection<Course> Courses)
                    .WithMany(c => c.Students)          // for the every Courses various ,it all has many Students various in it
    // (ICollection<Student> Students) .Map(cs => { cs.MapLeftKey("StudentRefId"); // a new column name StudentRefId for Student in StudentCourse table cs.MapRightKey("CourseRefId"); cs.ToTable("StudentCourse"); });

    Best regards,

    Jolie

    Wednesday, April 5, 2017 7:30 AM