LP1*_*P13 3 entity-framework-core ef-core-2.0
我有以下 SQL 表
ID INT
Status NVARCHAR(50)
FileContent XML
Run Code Online (Sandbox Code Playgroud)
使用 EF Core 我想选择ID
和Status
列但不加载 XML 列。(由于 xml 数据可能很大,我不想将其加载到内存中,而且我正在执行的业务逻辑不需要它)
,然后设置Status
public async Task DoSomwthing(int id)
{
// select only needed columns
var result = await _dbContext.MyTable
.Where(x=>x.ID == id)
.Select(x=> new
{
ID = x.ID,
Status = x.Status
})
.SingleOrDefaultAsync();
// do something here using result
// Since i am not loading the complete entity
// How do i set the Status here to new value
//Save the tracked changes
await _dbContext.SaveChangesAsync();
}
Run Code Online (Sandbox Code Playgroud)
除了将内容视为单独的相关实体的表拆分之外,EF 还支持仅更新选定的列。
因此,您可以使用其属性的子集构造一个实体,然后将未检索到的属性标记为未更改。例如:
public async Task DoSomething(int id)
{
// select only needed columns
var result = await this.Foos
.Where(x => x.ID == id)
.Select(x => new Foo
{
ID = x.ID,
Status = x.Status
})
.SingleOrDefaultAsync();
result.Status = 2;
this.Entry(result).State = EntityState.Modified;
this.Entry(result).Property(nameof(Foo.Content)).IsModified = false;
//Save the tracked changes
await this.SaveChangesAsync();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6028 次 |
最近记录: |