Ejemplo n.º 1
0
        public static void Demo_Projection_OneFlight()
        {
            using (var ctx = new WWWingsContext())
            {
                CUI.MainHeadline("Projection");

                var q = from x in ctx.FlightSet
                        where x.FlightNo == 101
                        orderby x.FlightNo
                        select new Flight()
                {
                    FlightNo    = x.FlightNo,
                    Date        = x.Date,
                    Departure   = x.Departure,
                    Destination = x.Destination,
                    Seats       = x.Seats,
                    FreeSeats   = x.FreeSeats,
                };

                var f = q.FirstOrDefault();

                Console.WriteLine(f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));
                ctx.Attach(f);
                Console.WriteLine(f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));
                f.FreeSeats++;
                Console.WriteLine(f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));


                var anz = ctx.SaveChanges();
                Console.WriteLine("Number of saved changes: " + anz);
            }
        }
Ejemplo n.º 2
0
        public static void Projection_Change()
        {
            using (var ctx = new WWWingsContext())
            {
                CUI.MainHeadline(nameof(Projection_Change));

                var q = (from f in ctx.FlightSet
                         where f.FlightNo > 100
                         orderby f.FlightNo
                         select new Flight()
                {
                    FlightNo = f.FlightNo,
                    Date = f.Date,
                    Departure = f.Departure,
                    Destination = f.Destination,
                    FreeSeats = f.FreeSeats,
                    Timestamp = f.Timestamp
                }).Take(2);


                var flightSet = q.ToList();

                foreach (var f in flightSet)
                {
                    Console.WriteLine($"Flight Nr {f.FlightNo} from {f.Departure} to {f.Destination} has {f.FreeSeats} free seats!");
                }

                Console.WriteLine("Number of flights: " + flightSet.Count);

                foreach (var f in flightSet)
                {
                    Console.WriteLine("Before attach: " + f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));
                    ctx.Attach(f);
                    Console.WriteLine("After attach: " + f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));
                    f.FreeSeats--;
                    Console.WriteLine("After Änderung: " + f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));

                    var anz = ctx.SaveChanges();
                    Console.WriteLine("Number of saved changes: " + anz);
                    Console.WriteLine("After saving: " + f + " State: " + ctx.Entry(f).State + " Timestamp: " + ByteArrayToString(f.Timestamp));
                }
            }
        }
        public static void RemoveFlightWithKey()
        {
            Console.WriteLine(nameof(RemoveFlightWithKey));

            using (WWWingsContext ctx = new WWWingsContext())
            {
                // Create a dummy object
                var f = new Flight();
                f.FlightNo = 123456;

                Console.WriteLine($"After creation: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State);

                // Append dummy object to context
                ctx.Attach(f);

                Console.WriteLine($"After attach: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State);

                // Delete flight
                ctx.FlightSet.Remove(f);
                // or: ctx.Remove(f);

                Console.WriteLine($"After remove: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State);

                try
                {
                    var anz = ctx.SaveChanges();
                    if (anz == 0)
                    {
                        Console.WriteLine("Problem: No changes saved!");
                    }
                    else
                    {
                        Console.WriteLine("Number of saved changes: " + anz);
                    }
                    Console.WriteLine($"After saving: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.ToString());
                }
            }
        }
        public static void TrackingMode_NoTracking_Attach()
        {
            CUI.MainHeadline(nameof(TrackingMode_NoTracking_Attach));

            CUI.Headline("Attach() before change");

            using (WWWingsContext ctx = new WWWingsContext())
            {
                var flightSet = ctx.FlightSet.AsNoTracking().ToList();
                var flight    = flightSet[0];
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Detached
                ctx.Attach(flight);
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Unchanged
                flight.FreeSeats--;
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Modified
                int count = ctx.SaveChanges();
                Console.WriteLine($"Saved changes: {count}");                            // 0
            }

            CUI.Headline("Attach() after change (change state per property)");
            using (WWWingsContext ctx = new WWWingsContext())
            {
                var flightSet = ctx.FlightSet.AsNoTracking().ToList();
                var flight    = flightSet[0];
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Detached
                flight.FreeSeats--;
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Detached
                ctx.Attach(flight);
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Unchanged
                // geändertes Attribut bei EFC melden
                ctx.Entry(flight).Property(f => f.FreeSeats).IsModified = true;
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Modified
                int count = ctx.SaveChanges();
                Console.WriteLine($"Saved changes: {count}");                            // 1
            }

            CUI.Headline("Attach() after change (change state per object)");
            using (WWWingsContext ctx = new WWWingsContext())
            {
                ctx.Log();
                var flightSet = ctx.FlightSet.AsNoTracking().ToList();
                var flight    = flightSet[0];
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Detached
                flight.FreeSeats--;
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Detached
                ctx.Attach(flight);
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Unchanged
                ctx.Entry(flight).State = EntityState.Modified;
                Console.WriteLine(flight + " object state: " + ctx.Entry(flight).State); // Modified
                int count = ctx.SaveChanges();
                Console.WriteLine($"Saved changes: {count}");                            // 1
            }

            CUI.Headline("Attach() with non entity class -- not allowed!!!");
            using (WWWingsContext ctx = new WWWingsContext())
            {
                var obj = new FileInfo(@"c:\temp\daten.txt");
                try
                {
                    ctx.Attach(obj); // Error: The entity type 'FileInfo' was not found. Ensure that the entity type has been added to the model.
                }
                catch (Exception e)
                {
                    CUI.PrintError(e);
                }
            }
        }