static void Main(string[] args)
        {
            using (CarContext db = new CarContext())
            {
                //db.Database.Connection.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=EFCodeFirst.CarContext;Integrated Security=True;MultipleActiveResultSets=True";
                Car c = new Car();
                c.Targa = "abc";
                c.Modello = "Alfa Romeo GT";

                Person p = new Person();
                p.FirstName = "Antonio";
                p.LastName = "Pelleriti";
                c.Person = p;

                db.Cars.Add(c);
                db.People.Add(p);

                db.SaveChanges();
            }
        }
Example #2
0
        /// <summary>
        /// The EF code first.
        /// </summary>
        /// <returns>the entity framework code first view.</returns>
        public ActionResult EFCodeFirst()
        {
            int count;
            int? newCount = null;

            EFContext context = null;
            using (MiniProfiler.Current.Step("EF Stuff"))
            {
                try
                {
                    using (MiniProfiler.Current.Step("Create Context"))
                        context = new EFContext();

                    // this is not correct, as the count from this assignment is never actually used
                    using (MiniProfiler.Current.Step("First count"))
                        count = context.People.Count();

                    using (MiniProfiler.Current.Step("Insertion"))
                    {
                        var p = new Person { Name = "sam" };
                        context.People.Add(p);
                        context.SaveChanges();
                    }

                    // this count is actually used.
                    using (MiniProfiler.Current.Step("Second count"))
                        count = context.People.Count();

                    const string sql = "Select count(*) from People";
                    using (MiniProfiler.Current.Step("Get Count from SqlQuery Method - no sql recorded"))
                    {
                        newCount = context.Database.SqlQuery<int>(sql).Single();
                    }
                    using (MiniProfiler.Current.Step("Get Count using ProfiledConnection - sql recorded"))
                    {
                        using (var conn = new ProfiledDbConnection(context.Database.Connection, MiniProfiler.Current))
                        {
                            conn.Open();
                            newCount = conn.Query<int>(sql).Single();
                            conn.Close();
                        }
                    }

                }
                finally
                {
                    if (context != null)
                    {
                        context.Dispose();
                    }
                }
            }

            return Content(string.Format("EF Code First complete - count: {0}, sqlQuery count {1}", count, newCount));
        }
Example #3
0
        /// <summary>
        /// The EF code first.
        /// </summary>
        /// <returns>the entity framework code first view.</returns>
        public ActionResult EFCodeFirst()
        {
            int count;

            EFContext context = null;
            using (MiniProfiler.Current.Step("EF Stuff"))
            {
                try
                {
                    using (MiniProfiler.Current.Step("Create Context"))
                        context = new EFContext();

                    // this is not correct, as the count from this assignment is never actually used
                    using (MiniProfiler.Current.Step("First count"))
                        count = context.People.Count();

                    using (MiniProfiler.Current.Step("Insertion"))
                    {
                        var p = new Person { Name = "sam" };
                        context.People.Add(p);
                        context.SaveChanges();
                    }

                    // this count is actually used.
                    using (MiniProfiler.Current.Step("Second count"))
                        count = context.People.Count();
                }
                finally
                {
                    if (context != null)
                    {
                        context.Dispose();
                    }
                }
            }

            return Content("EF Code First complete - count: " + count);
        }