none
Create an new record in a different table from your entity model that the data is coming from!! RRS feed

  • Question

  • Hi folks, is it possible to create an new record from results from my entity mode, basically I would like to take the results from my entity model and store them in a different table/entity model.

    Still learning here, sorry if I’m asking a dumb question J

    pivate List<DataMode> Loadorder()
            {
                using (tableA dbcontext = new tableA())
    
                    return (from Orders in dbcontext.Orders
    
                            join customer in dbcontext.Customers on Orders.CustomerID equals customer.CustomerID
                            join link_orderproduct in dbcontext.Link_OrderProduct on Orders.OrderID equals link_orderproduct.OrderID
                            join product in dbcontext.Products on link_orderproduct.ProductID equals product.ProductID
                            join garage in dbcontext.Garages on link_orderproduct.GarageID equals garage.GarageID
                            where Orders.OrderID == orderId.Value
    
                            select new DataMode
                            {
                                
                                OrderID = Orders.OrderID,
                                Name = customer.Name,
                                ContactNo = customer.ContactNo,
                                Registration = customer.Registration,
    
                                Brand = product.Brand,
                                TyreModel = product.TyreModel,
                                Width = (int) product.Width,
                                Profile = (int) product.Profile,
                                Diamete = (int) product.Diamete,
                                Speed = product.Speed,
    
                                FittingDate = (DateTime)link_orderproduct.FittingDate,
                                ProductUnitSalePrice = (decimal) link_orderproduct.Product.ProductUnitSalePrice,
                                ProductQuantity = (int) link_orderproduct.ProductQuantity,
                                TotalProductSaleCost = (decimal)link_orderproduct.TotalProductSaleCost,
                                //MOTChkbx = (bool) link_orderproduct.MOTChkbx,
    
                                AirConRecharge = (decimal) garage.AirConRecharges,
                                WheelTracking = (decimal) garage.WheelTrackings,
                                MOTS = (decimal) garage.MOTS
    
                            }).ToList();
            }
    
    
    private void newRecordInTempTable()
            {
    
                using (tableB dbcontext = new tableB())
    
                {
                    var query = Loadorder();
    }






    • Edited by brucey54 Monday, November 23, 2015 9:51 PM
    Monday, November 23, 2015 9:45 PM

Answers

  • The short answer is yes, it can be done.  If your DataMode class is the model for an entity in your tableB database context, then all you would have to do is create a new instance of the corresponding tableB entity for each item in the List returned by Loadorder():

    foreach (var item in query)
    {
        dbcontext.DataModes.Add(item);
        dbcontext.SaveChanges();
    }

    If your tableB context has a different object, then you would just need to create a new instance of the matching entity, and assign each of the properties of the DataMode object to the corresponding properties of your entity, then call the Add method of the tableB table entity, then call the SaveChanges method:

    foreach (var item in query)
    {
        var tableBEntity = dbcontext.YourEntityName;
    
        tableBEntity.OrderID = item.OrderID;
        tableBEntity.Name = item.Name;
    
        // And so on, until all of the properties of the item have been copied.
    
        dbcontext.YourTableName.Add(tableBEntity);
        dbcontext.SaveChanges();
    }
    Of course, not knowing your entities, the above is more psuedocode than working C#, but that's the basic process.


    • Marked as answer by brucey54 Thursday, November 26, 2015 10:40 AM
    Monday, November 23, 2015 10:14 PM
  • Yes, you can take the List<DataMode> from your method and pass it in as parm to another method with that method having a different DBContext using other tables. You would have cto reate a instance new the Entity you are working with in the other DBContext  and start mapping property by property to from entity to another entity that are on the model.

    • Marked as answer by brucey54 Thursday, November 26, 2015 10:40 AM
    Monday, November 23, 2015 10:16 PM

All replies

  • The short answer is yes, it can be done.  If your DataMode class is the model for an entity in your tableB database context, then all you would have to do is create a new instance of the corresponding tableB entity for each item in the List returned by Loadorder():

    foreach (var item in query)
    {
        dbcontext.DataModes.Add(item);
        dbcontext.SaveChanges();
    }

    If your tableB context has a different object, then you would just need to create a new instance of the matching entity, and assign each of the properties of the DataMode object to the corresponding properties of your entity, then call the Add method of the tableB table entity, then call the SaveChanges method:

    foreach (var item in query)
    {
        var tableBEntity = dbcontext.YourEntityName;
    
        tableBEntity.OrderID = item.OrderID;
        tableBEntity.Name = item.Name;
    
        // And so on, until all of the properties of the item have been copied.
    
        dbcontext.YourTableName.Add(tableBEntity);
        dbcontext.SaveChanges();
    }
    Of course, not knowing your entities, the above is more psuedocode than working C#, but that's the basic process.


    • Marked as answer by brucey54 Thursday, November 26, 2015 10:40 AM
    Monday, November 23, 2015 10:14 PM
  • Yes, you can take the List<DataMode> from your method and pass it in as parm to another method with that method having a different DBContext using other tables. You would have cto reate a instance new the Entity you are working with in the other DBContext  and start mapping property by property to from entity to another entity that are on the model.

    • Marked as answer by brucey54 Thursday, November 26, 2015 10:40 AM
    Monday, November 23, 2015 10:16 PM