EntityFramework 学习 一 DBEntityEntry

DbEntityEntry是一个重要的类,用来获取各种各样的实体信息

可以通过DBContext的Entry方法获取DbEntityEntry的实例

DBEntityEntry studentEntry = dbcontext.Entry(StudentEntity);

 

通过DBEntityEntry,可以获取实体的状态,所有属性的当前值和原始值

using (var dbCtx = new SchoolDBEntities())
{
    //get student whose StudentId is 1
    var student = dbCtx.Students.Find(1);

    //edit student name
    student.StudentName = "Edited name";

    //get DbEntityEntry object for student entity object
    var entry = dbCtx.Entry(student);

    //get entity information e.g. full name
    Console.WriteLine("Entity Name: {0}", entry.Entity.GetType().FullName);

    //get current EntityState
    Console.WriteLine("Entity State: {0}", entry.State );

    Console.WriteLine("********Property Values********");

    foreach (var propertyName in entry.CurrentValues.PropertyNames )
    {
        Console.WriteLine("Property Name: {0}", propertyName);

        //get original value
        var orgVal = entry.OriginalValues[propertyName];
        Console.WriteLine("     Original Value: {0}", orgVal);
                    
        //get current values
        var curVal = entry.CurrentValues[propertyName];
        Console.WriteLine("     Current Value: {0}", curVal);
    }

}
    

 

DbEntityEntry可以设置实体的状态如Added、Modified或Deleted

context.Entry(student).State = System.Data.Entity.EntityState.Modified;

 

Method NameReturn TypeDescription
CollectionDBCollectionEntryGets an object that represents the collection navigation property from this entity to a collection of related entities.

Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var collectionProperty = studentDBEntityEntry.Collection<course>(s => s.Courses);
ComplexPropertyDBComplexPropertyEntryGets an object that represents a complex property of this entity. 
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var complexProperty = studentDBEntityEntry.ComplexProperty(stud.StudentStandard);
GetDatabaseValuesDBPropertyValuesQueries the database for copies of the values of the tracked entity as they currently exist in the database. Changing the values in the returned dictionary will not update the values in the database. If the entity is not found in the database then null is returned.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity);
var dbPropValues = studentDBEntityEntry.GetDatabaseValues();
PropertyDBPropertyEntryGets an object that represents a scalar or complex property of this entity.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
string propertyName = studentDBEntityEntry.Property("StudentName").Name;
ReferenceDBReferenceEntryGets an object that represents the reference (i.e. non-collection) navigation property from this entity to another entity.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity); 
var referenceProperty = studentDBEntityEntry.Reference(s => s.Standard);
ReloadvoidReloads the entity from the database overwriting any property values with values from the database. The entity will be in the Unchanged state after calling this method.
Example:
var studentDBEntityEntry = dbContext.Entry(studentEntity);
studentDBEntityEntry.Reload();

 

转载于:https://www.cnblogs.com/lanpingwang/p/6617946.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值