复制代码 代码如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
region.RegionDescription = "Test";
context.SaveChanges();
}
复制代码 代码如下:
SQL1:SELECT TOP 1 [Extent1].[RegionID] AS [RegionID], [Extent1].[RegionDescription] AS [RegionDescription]FROM [dbo].[Region] AS [Extent1]WHERE 4 = [Extent1].[RegionID]
SQL2:exec sp_executesql N'update [dbo].[Region]set [RegionDescription] = @0where ([RegionID] = @1)', N'@0 nchar(4),@1 int', @0 = N'Test', @1 = 4
复制代码 代码如下:
Region region;
using (NorthwindEntities context = new NorthwindEntities())
{
region = context.Region.FirstOrDefault(v => v.RegionID == 4);
}
using (NorthwindEntities context = new NorthwindEntities())
{
region.RegionDescription = "Test";
context.SaveChanges();
}
复制代码 代码如下:
Region region;
using (NorthwindEntities context = new NorthwindEntities())
{
region = context.Region.FirstOrDefault(v => v.RegionID == 4);
}
using (NorthwindEntities context = new NorthwindEntities())
{
Region newRegion = context.Region.FirstOrDefault(v => v.RegionID == region.RegionID);
region.RegionDescription = "Test";
context.SaveChanges();
}
复制代码 代码如下:
Region region;
using (NorthwindEntities ne = new NorthwindEntities())
{
//利用EntityObject.Execute(MergeOption.NoTracking),等效于使用ObjectContext.Dettach(EntityObject)
//查询并分离对象
region = ne.Region.Execute(MergeOption.NoTracking).Where(v => v.RegionID == 1).FirstOrDefault();
}
//修改分离的值
region.RegionDescription = "TestTest1";
//使用分离的对象 order 更新
using (NorthwindEntities context = new NorthwindEntities())
{
//将数据载入到context中以便更新
context.GetObjectByKey(region.EntityKey);
//使用order 更新 context中的对应对象
context.ApplyPropertyChanges(region.EntityKey.EntitySetName, region);
context.SaveChanges();
}
复制代码 代码如下:
SQL1:exec sp_executesql N'SELECT [Extent1].[RegionID] AS [RegionID], [Extent1].[RegionDescription] AS [RegionDescription] FROM [dbo].[Region] AS [Extent1] WHERE [Extent1].[RegionID] = @p0', N'@p0 int', @p0 = 1
SQL2:exec sp_executesql N'update [dbo].[Region] set [RegionDescription] = @0where ([RegionID] = @1) ', N'@0 nchar(9),@1 int', @0 = N'TestTest1', @1 = 1
复制代码 代码如下:
using (NorthwindEntities context = new NorthwindEntities())
{
Region region = context.Region.FirstOrDefault(v => v.RegionID == 4);
context.Detach(region);
region.RegionDescription = "因为测试";
context.Attach(region);
var newRegion = context.ObjectStateManager.GetObjectStateEntry(region);
newRegion.SetModified();
newRegion.SetModifiedProperty("RegionDescription");
context.SaveChanges();
}
复制代码 代码如下:
exec sp_executesql N'update [dbo].[Region]set [RegionDescription] = @0where ([RegionID] = @1)', N'@0 nchar(4),@1 int', @0 = N'因为测试', @1 = 4
新闻热点
疑难解答