EF step operation of the database and some simple manipulation statements https://www.cnblogs.com/cjm123/p/9681505.html

https://www.cnblogs.com/cjm123/p/9681505.html

  Here was written for my own records, and will not write a good blog, it will not top, if you have friends to see, and I think the content inside is not ye, hope forgive me ha!

About this part, I recommended here a summary of a very good blog, if you click came in, then please skip the following, just click on this link, because writing is really good: HTTPS: //www.cnblogs. COM / gosky / the p-/ 5751815.html .

  I personally have no relevant content EF detailed finishing, so the reference to the significance of this essay is not, just stick some code up, after the opportunity to have time, will make a summary of relevant content.

EF detailed steps to use database operations are divided into what steps:

1. Create a database model EF Entity:

Right-click on an item to add == "data ==" ADO.Net Entity database model == "and follow the prompts to go;

 

2. Examples of context

  First, find the need to instantiate the name of the context, this document ef entity in the inside:

 

 After opening the file, as follows:

 

 Red box is the name of the context of the entity.

Examples of specific context, the following:

FirstDBEntities1 db = new FirstDBEntities1();

 

3. Use the query context of the operation of the database

(1) id obtain data in a table according to the master key

Methods of operation are:

UserInfo userInfo = new UserInfo();

userInfo  =  db.UserInfo.Find(id);

 

 Wherein UserInfo entity class is the class name corresponding to the Table, after performing the operation, to obtain the primary key is the id data of a number of the line, and then to get to the appropriate field by userInfo.XXX value;

(2) get all the data in a data table

Methods of operation are:

List<UserInfo>  list = new List<UserInfo>

list = db.UserInfo.ToList();

 

 执行该操作后久会把该数据表中的所有数据都取出来,存放在一个List集合中;

(3)根据其他条件进行查询

var user = db.UserInfo.Where(u => u.userName == "AAA" );

 

 这是使用lambda表达式进行查询,查询的结果是一个list集合,通过下面的方法可以遍历取出这些数据:

foreach(var item in user)
{
      xxx = item.yyyy;          
}

 

 4.使用上下文操作数据库 之 增

创建一个实体数据对象,并对其各个字段赋值,然后使用上下文执行增加操作,

Copy the code
UserInfo user = new UserInfo();
user.userName = "BBB";
user.passWord = "23456";
db.UserInfo.Add(user);

db.SaveChanges();
Copy the code

 

需要注意的是,在执行完添加操作之后,需要执行 db.SaveChanges() 操作, 这句话的意思是,把我们修改的内容更新到数据库中。

5.使用长下文操作数据库 之 改

首先要指定你修改的某一行的id:

UserInfo user = new UserInfo();
user.UserId = 3;

 

 接下来对要修改的字段进行重新赋值:

user.userName = "CCC";
user.passWord = "1234567";

 

然后执行修改操作:

 

db.Entry(user).State = EntityState.Modified;

 

 最后将修改保存到数据库中

db.SaveChanges();

 

 

 如果只修改单个属性,比如只修改userName,则写为:

UserInfo user = new UserInfo();
user.userId = 3;
user.userName = "DDD";
db.Entry(user ).Property(u => u.userName).IsModified = true;
db.SaveChanges();

 

 6. 使用长下文操作数据库 之 删

Copy the code
UserInfo user = new UserInfo();
user.userId = 3;

db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Deleted;

db.SaveChanges();
Copy the code

  这里是写给我自己做记录的,不会写成一篇很好的博客,也不会置顶,如果有朋友看到了,而且觉得里面的内容不咋的,希望见谅哈!

关于这部分内容,这里推荐一篇总结的非常好的博客,如果你点击进来了,那么请略过下面的内容,直接点击这个链接,因为写的真的不错:https://www.cnblogs.com/gosky/p/5751815.html

  我个人还没有对EF相关的内容进行详细的整理,所以这篇随笔的参考意义不大,只是贴一些代码上去,以后有机会有时间,会对相关内容做一个总结。

使用EF操作数据库的详细步骤主要分为一下几步:

1.创建EF实体数据库模型:

在某个项目上点击右键 添加 ==》数据 ==》ADO.Net实体数据库模型 ==》然后按照提示去走;

 

2.实例化上下文

  首先找到需要实例化的上下文的名字,在ef实体里的这个文件里:

 

 打开该文件后,代码如下:

 

 红框处的名字就是该实体的上下文。

实例化上下文的具体做法如下:

FirstDBEntities1 db = new FirstDBEntities1();

 

3.使用上下文操作数据库 之 查询操作

(1)根据主键id获取某个表的数据

操作方法为:

UserInfo userInfo = new UserInfo();

userInfo  =  db.UserInfo.Find(id);

 

 其中UserInfo是该数据表对应的实体类的类名,执行该操作后,即可获取主键id为某个数的那一行的数据,然后通过 userInfo.XXX 即可获去相应字段的值;

(2)获取某个数据表全部数据

操作方法为:

List<UserInfo>  list = new List<UserInfo>

list = db.UserInfo.ToList();

 

 执行该操作后久会把该数据表中的所有数据都取出来,存放在一个List集合中;

(3)根据其他条件进行查询

var user = db.UserInfo.Where(u => u.userName == "AAA" );

 

 这是使用lambda表达式进行查询,查询的结果是一个list集合,通过下面的方法可以遍历取出这些数据:

foreach(var item in user)
{
      xxx = item.yyyy;          
}

 

 4.使用上下文操作数据库 之 增

创建一个实体数据对象,并对其各个字段赋值,然后使用上下文执行增加操作,

Copy the code
UserInfo user = new UserInfo();
user.userName = "BBB";
user.passWord = "23456";
db.UserInfo.Add(user);

db.SaveChanges();
Copy the code

 

需要注意的是,在执行完添加操作之后,需要执行 db.SaveChanges() 操作, 这句话的意思是,把我们修改的内容更新到数据库中。

5.使用长下文操作数据库 之 改

首先要指定你修改的某一行的id:

UserInfo user = new UserInfo();
user.UserId = 3;

 

 接下来对要修改的字段进行重新赋值:

user.userName = "CCC";
user.passWord = "1234567";

 

然后执行修改操作:

 

db.Entry(user).State = EntityState.Modified;

 

 最后将修改保存到数据库中

db.SaveChanges();

 

 

 如果只修改单个属性,比如只修改userName,则写为:

UserInfo user = new UserInfo();
user.userId = 3;
user.userName = "DDD";
db.Entry(user ).Property(u => u.userName).IsModified = true;
db.SaveChanges();

 

 6. 使用长下文操作数据库 之 删

Copy the code
UserInfo user = new UserInfo();
user.userId = 3;

db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Deleted;

db.SaveChanges();
Copy the code

  这里是写给我自己做记录的,不会写成一篇很好的博客,也不会置顶,如果有朋友看到了,而且觉得里面的内容不咋的,希望见谅哈!

关于这部分内容,这里推荐一篇总结的非常好的博客,如果你点击进来了,那么请略过下面的内容,直接点击这个链接,因为写的真的不错:https://www.cnblogs.com/gosky/p/5751815.html

  我个人还没有对EF相关的内容进行详细的整理,所以这篇随笔的参考意义不大,只是贴一些代码上去,以后有机会有时间,会对相关内容做一个总结。

使用EF操作数据库的详细步骤主要分为一下几步:

1.创建EF实体数据库模型:

在某个项目上点击右键 添加 ==》数据 ==》ADO.Net实体数据库模型 ==》然后按照提示去走;

 

2.实例化上下文

  首先找到需要实例化的上下文的名字,在ef实体里的这个文件里:

 

 打开该文件后,代码如下:

 

 红框处的名字就是该实体的上下文。

实例化上下文的具体做法如下:

FirstDBEntities1 db = new FirstDBEntities1();

 

3.使用上下文操作数据库 之 查询操作

(1)根据主键id获取某个表的数据

操作方法为:

UserInfo userInfo = new UserInfo();

userInfo  =  db.UserInfo.Find(id);

 

 其中UserInfo是该数据表对应的实体类的类名,执行该操作后,即可获取主键id为某个数的那一行的数据,然后通过 userInfo.XXX 即可获去相应字段的值;

(2)获取某个数据表全部数据

操作方法为:

List<UserInfo>  list = new List<UserInfo>

list = db.UserInfo.ToList();

 

 执行该操作后久会把该数据表中的所有数据都取出来,存放在一个List集合中;

(3)根据其他条件进行查询

var user = db.UserInfo.Where(u => u.userName == "AAA" );

 

 这是使用lambda表达式进行查询,查询的结果是一个list集合,通过下面的方法可以遍历取出这些数据:

foreach(var item in user)
{
      xxx = item.yyyy;          
}

 

 4.使用上下文操作数据库 之 增

创建一个实体数据对象,并对其各个字段赋值,然后使用上下文执行增加操作,

Copy the code
UserInfo user = new UserInfo();
user.userName = "BBB";
user.passWord = "23456";
db.UserInfo.Add(user);

db.SaveChanges();
Copy the code

 

需要注意的是,在执行完添加操作之后,需要执行 db.SaveChanges() 操作, 这句话的意思是,把我们修改的内容更新到数据库中。

5.使用长下文操作数据库 之 改

首先要指定你修改的某一行的id:

UserInfo user = new UserInfo();
user.UserId = 3;

 

 接下来对要修改的字段进行重新赋值:

user.userName = "CCC";
user.passWord = "1234567";

 

然后执行修改操作:

 

db.Entry(user).State = EntityState.Modified;

 

 最后将修改保存到数据库中

db.SaveChanges();

 

 

 如果只修改单个属性,比如只修改userName,则写为:

UserInfo user = new UserInfo();
user.userId = 3;
user.userName = "DDD";
db.Entry(user ).Property(u => u.userName).IsModified = true;
db.SaveChanges();

 

 6. 使用长下文操作数据库 之 删

Copy the code
UserInfo user = new UserInfo();
user.userId = 3;

db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Deleted;

db.SaveChanges();
Copy the code

Guess you like

Origin www.cnblogs.com/wfy680/p/11969302.html