Example #1
0
        public void AddCourse(string name, int credits)
        {
            var course = new Course()
            {
                CourseId = 1, Name = name, Credits = credits
            };

            using (var dbcontext = new CodeFirstDBContext())
            {
                dbcontext.Courses.Add(course);
                dbcontext.SaveChanges();
            }
        }
Example #2
0
        /// <summary>
        /// 使用CodeFirst时,如果第一次创建生成了数据库,之后再增加实体类,直接执行的话,就会报异常,说是数据迁移问题。具体看看https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/migrations/index  这个是完整的EF教程
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            CodeFirstDBContext dbcontext = new CodeFirstDBContext();

            dbcontext.Database.CreateIfNotExists();
            UserInfo userInfo = new UserInfo();

            userInfo.UserName = "******";
            userInfo.Email    = "aaa";
            dbcontext.UserInfo.Add(userInfo);
            dbcontext.SaveChanges();
            Console.Write("成功");
            Console.ReadKey();
        }
Example #3
0
        static void Main(string[] args)
        {
            using (var context = new CodeFirstDBContext())
            {
                var Student = new Student()
                {
                    Name = "Paul"
                };
                context.Students.Add(Student);
                context.SaveChanges();
            }
            Console.ReadKey();

            /*
             * (1) Enable-Migrations
             * (2) Add-Migration Version_Name -ConnectionString "data source=.\sqlexpress;initial catalog=codefirstDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
             *
             * (3) Update-Database -ConnectionString "data source=.\sqlexpress;initial catalog=codefirstDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
             *
             */
        }