C# -Entity Framework添加数据(增)

之前操作步骤见《C#-创建Database First Demo》:https://blog.csdn.net/sinat_40003796/article/details/100541514

 

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Infrastructure;//使用方法二,必须添加的命名空间

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (testEntities db = new testEntities())
            {
                student st = new student
                {
                    name = "mark",
                    age = 12
                };
                //方法一
                //db.student.Add(st);
                //方法二
                DbEntityEntry entry = db.Entry(st);
                entry.State = System.Data.Entity.EntityState.Added;
                db.SaveChanges();
            }
        }
    }
}

运行结果:

C# -Entity Framework添加数据(增)_第1张图片

你可能感兴趣的:(C#Entity,Framework)