Exemple #1
0
 /// <summary>
 /// 保存.
 /// </summary>
 /// <param name="data"></param>
 public void SaveCarData(Car data)
 {
     if (data.CarID == 0)
     {
         context.CarDbSet.Add(data);
     }
     else
     {
         context.Entry(data).State = EntityState.Modified;
     }
     context.SaveChanges();
 }
        static void Main(string[] args)
        {
            Test t = new Test();

            Car c1 = new Car()
            {
                CarName = "BMW",
                Engine = new Engine()
                {
                    EngineName = "BMW发动机"
                }
            };

            Car c2 = new Car()
            {
                CarName = "奥迪",
                Engine = new Engine()
                {
                    EngineName = "奥迪发动机"
                }
            };


            t.SaveCarData(c1);
            t.SaveCarData(c2);



            foreach (Engine e in t.EngineDataSource)
            {
                Console.WriteLine("{0} 安装在 {1} 上.",
                    e.EngineName, e.OnCar.CarName);
            }

            Console.ReadLine();
        }