using System;
using System.Collections.Generic; public partial class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
} public int StudentID { get; set; }
public string StudentName { get; set; }
public Nullable<int> StandardId { get; set; }
public byte[] RowVersion { get; set; } public virtual Standard Standard { get; set; }
public virtual StudentAddress StudentAddress { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq; public partial class SchoolDBEntities : DbContext
{
public SchoolDBEntities()
: base("name=SchoolDBEntities")
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ } public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<Standard> Standards { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<StudentAddress> StudentAddresses { get; set; }
public virtual DbSet<Teacher> Teachers { get; set; }
}
class Program
{
static void Main(string[] args)
{
// create new Student entity object in disconnected scenario (out of the scope of DbContext)
var newStudent = new Student(); //set student name
newStudent.StudentName = "Bill"; //create DBContext object
using (var dbCtx = new SchoolDBEntities())
{
//Add Student object into Students DBset
dbCtx.Students.Add(newStudent); // call SaveChanges method to save student into database
dbCtx.SaveChanges();
}
}
}

首先创建新的Student对象并设置StudentName为bill

其次创建新的Context上下文,并把新实体添加到Students的EntitySet中

最后调用SaveChanges方法,将在数据库中执行如下操作

exec sp_executesql N'INSERT [dbo].[Student]([StudentName], [StandardId])
VALUES (@0, NULL)
SELECT [StudentID], [RowVersion]
FROM [dbo].[Student]
WHERE @@ROWCOUNT > 0 AND [StudentID] = scope_identity(),@0='Bill'

上面也可以用如下方法添加实体

class Program
{
static void Main(string[] args)
{
// create new Student entity object in disconnected scenario (out of the scope of DbContext)
var newStudent = new Student(); //set student name
newStudent.StudentName = "Bill"; //create DBContext object
using (var dbCtx = new SchoolDBEntities())
{
//Add newStudent entity into DbEntityEntry and mark EntityState to Added
dbCtx.Entry(newStudent).State = System.Data.Entity.EntityState.Added; // call SaveChanges method to save new Student into database
dbCtx.SaveChanges();
}
}
}

EntityFramework 学习 一 Add New Entity using DBContext in Disconnected Scenario的更多相关文章

  1. EntityFramework 学习 一 Update Existing Entity using DBContext in Disconnected Scenario

    using System; using System.Collections.Generic; public partial class Student { public Student() { th ...

  2. EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario

    Student studentToDelete; . Get student from DB using (var ctx = new SchoolDBEntities()) { studentToD ...

  3. EntityFramework 学习 一 Add Entity Graph using DbContext:

    //Create student in disconnected mode Student newStudent = new Student() { StudentName = "New S ...

  4. EntityFramework 学习 一 Persistence in Entity Framework

    实体框架的持久化 当用EntityFramework持久化一个对象时,有两种情形:连接的和断开的 1.连接场景:使用同一个context上下文从数据库中查询和持久化实体时,查询和持久化实体期间,con ...

  5. EntityFramework 学习 一 Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0

    To migrate your existing Entity Framework 4.x project to Entity Framework 5.0 using VS2012, first ta ...

  6. Entity Framework Tutorial Basics(23):Add Single Entity

    Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...

  7. Entity Framework DBContext 增删改查深度解析

    Entity Framework DBContext 增删改查深度解析 有一段时间没有更新博客了,赶上今天外面下雨,而且没人约球,打算把最近对Entity Framework DBContext使用的 ...

  8. EntityFramework 学习 一 DbSet

    DBSet类表示一个实体的集合,用来创建.更新.删除.查询操作,DBSet<TEntity>是DBSet的泛型版本 你可以使用DbContext获取DBSet的引用,例如dbContext ...

  9. DbContextScope,A simple and flexible way to manage your Entity Framework DbContext instances,by mehdime

    DbContextScope A simple and flexible way to manage your Entity Framework DbContext instances. DbCont ...

随机推荐

  1. jar包解压与打包

    首先感谢大神的指导:https://blog.csdn.net/mr_pang/article/details/47028921 1.首先准备一个能运行的jar文件,我们使用第三方解压工具进行解压wi ...

  2. Windows下UEFI环境的搭建

    Windows下UEFI环境的搭建 一.环境准备 1. 安装2012及以上VS https://visualstudio.microsoft.com/ 2.下载NASM 2.13.03  http:/ ...

  3. python中常用的base64 md5 aes des crc32等的加密解密

    1.base64 Python内置的base64模块可以实现base64.base32.base16.base85.urlsafe_base64的编码解码,python 3.x通常输入输出都是二进制形 ...

  4. linux上复制行到另一个文件

    1.复制3行到5行数据到ab.sh文件,不覆盖ab.sh文件中的已有内容 :3,5w>>ab.sh 2.复制3行到5行数据到ab.sh文件,覆盖ab.sh文件中的已有内容 :3,5w!ab ...

  5. 利用asset存储mesh

    做mesh导出的时候遇到了这个问题. 最后解决: 存储mesh数据:AssetDatabase.CreateAsset(meshfilter.mesh, "Assets/" + & ...

  6. 批处理--复制,解压文件,goto,nul

    rem 复制文件 copy "D:\xxxx" "C:\xxxx" rem 复制文件夹 xcopy "D:\xxxx" "C:\x ...

  7. Java final关键字特点

    一.特点 1.由于继承,方法可以重写,所以父类的功能就会被子类覆盖2.有时候我们不想子类覆盖父类的功能,这时候我们可以使用final关键字3.final可以修饰:类.变量,方法.4.final修饰类, ...

  8. Unity3d 快捷键

    Windows系统Unity3D中的快捷键 组合键 键 功能 File 文件 Ctrl   N New Scene 新建场景 Ctrl   O Open Scene 打开场景 Ctrl   S Sav ...

  9. FineUIPro中如何支持多语言(全局资源文件和本地资源文件)

    一个客户在邮件中问到了FineUIPro的多语言实现问题,其实 FineUIPro 并没有对此做特殊处理,因此直接使用 ASP.NET 原生支持的资源文件就能实现. 下面我们就以FineUIPro的空 ...

  10. Intersection(计算几何)

    Intersection Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)To ...