locked
Having problems with inserting the data into the database RRS feed

  • General discussion

  • Hello,

    I Have problem with inserting the data in the database I created 3 classes Routes, Bus and Point of interest. the routes will have multiple buses and the buses will have multiple points of interest(Locations) I am not sure if what I did is true or wrong

    public class Route { public Route() { Buses = new List<Bus>(); } public int Id { get; set; } public double destLong { get; set; } public double destLat { get; set; } public ICollection<Bus> Buses { get; set; } }

    public Bus()
            {
                POI = new List<PointsOfInterests>();
                routeID = new Route();
            }
            public int Id { get; set; }
            public double time { get; set; }
            public double srcLong { get; set; }
            public double srcLat { get; set; }
    
            public Route routeID { get; set; }
    
            public ICollection<PointsOfInterests> POI { get; set; }
        }
     public BusMapper() 
            {
                this.ToTable("Buses");
    
                this.HasKey(b => b.Id);
                this.Property(b => b.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
                this.Property(b => b.Id).IsRequired();
    
                this.Property(b => b.time).IsRequired();
    
                this.Property(b => b.srcLong).IsRequired();
    
                this.Property(b => b.srcLat).IsRequired();
                
                this.HasRequired(b => b.routeID).WithMany().Map(r => r.MapKey("RouteID"));
            }

    now the problem is my solution ok or not and plz can some one help me with insert the data just 1 data on the Point of interest and 1 data on the buses

    I tried with seeding

    var route1 = new Route()
                    {
                        Id = 1,
                        destLong = 35.933719,
                        destLat = 31.992418
                    };
                    var route2 = new Route()
                    {
                        Id = 2,
                        destLong = 35.868576,
                        destLat = 32.014975
                    };
    
                    _ctx.Routes.Add(route1);
                    _ctx.Routes.Add(route2);
                    _ctx.SaveChanges();
    
                    var bus1 = new Bus()
                    {
                        Id = 1,
                        time = 0,
                        srcLong = 0.0,
                        srcLat = 0.0,
                     // the problem is here how to add a
    //bus into the route for example route1 based on his ID
    //now when I write routeID = 1 conot convert from int
    //to route    
                    };

    so plz can some one just show me how to add the  bus into the route to have sth like that

    {
    Routes:{
      id =1,
      ...
      Busses:{
         id=1,
         ...
         Pointsofinterest:{
         id=1,
         ...
         }
      }
    }
    }

    and here is the link for the code.

    https://onedrive.live.com/redir?resid=AC25D7A2DC192BDB!15017&authkey=!APaRAd4RlQITi8w&ithint=folder%2crar

    Regards,

    Ayesh.



    Monday, October 26, 2015 5:10 PM

All replies

  • If the database table primary keys are auto incremented Identity keys, then all you do is create the parent object and leave the ID property in the object alone. You do the same for the children objects and their primary keys by leaving the ID property alone.

    If the ID(s) are auto incremented Identity keys on the tables, then by the ID property in the entity/object is = 0, then that tells EF that the object is to be inserted into the table. If ID is > 0, then that tells EF to find the record with the ID and update the record. 

    You leave the foreign key properties alone too, because EF will take the parent key and place it in the children objects by itself upon a child object insert.

    Monday, October 26, 2015 9:11 PM
  • But can I do it like ??

    var bus1 = new Bus() { Id = 1, time = 0, srcLong = 0.0, srcLat = 0.0, routeID = new Route(){

    Id = 1,

    destLong = 35.933719,

                           destLat = 31.992418

    } };



    Tuesday, October 27, 2015 3:53 PM