EF Core 2.0中是否存在针对CRUD的最佳实践?
有什么方法可以自动为模型自动生成CRUD,最好使用这些最佳实践?
举例来说,这是我目前拥有的东西,但是我必须将其复制到其他20个实体中:
[Route("entities")]
public class EntitiesController : Controller
{
private readonly ProjectContext _context;
public EntitiesController(ProjectContext context)
{
_context = context;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]Entities entity)
{
_context.Add(entity);
return Json(await _context.SaveChangesAsync() >= 1);
}
[HttpGet]
public JsonResult Get(int? entityId)
{
if (entityId == null)
return Json(_context.Entities.Select(x => x));
else
return Json(_context.Entities.FirstOrDefault(x => x.EntityId == entityId));
}
[HttpPut]
public async Task<IActionResult> Put([FromBody]Entities entity)
{
if (entity.EntityId < 1)
return BadRequest();
_context.Entities.Update(entity);
return Json(await _context.SaveChangesAsync() >= 1);
} …Run Code Online (Sandbox Code Playgroud) 将我的API部署到AWS Elastic Beanstalk后出现此错误。
重现步骤
附加信息
日志
2018-01-15T13:27:21.000Z错误0:(0)IIS AspNetCore模块-具有物理根目录'C:\ inetpub \ AspNetCoreWebApps \ app \'的应用程序'MACHINE / WEBROOT / APPHOST / DEFAULT WEB SITE'无法启动命令行'dotnet。\ WebApplication2.dll'处理,ErrorCode ='0x80004005:8000808c。
我是测试新手。我试过这个,但有一个例外。
@Mock
private Context context;
...
when(service.getResult(any(), context)).thenReturn(new ArrayList<>());
Run Code Online (Sandbox Code Playgroud)
例外:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
参数匹配器的使用无效!
预计 2 个匹配者,1 个记录:
我有一个组件,它根据客户端设备大小切换组件的模板。组件代码为:
import {Component} from '@angular/core';
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
@Component({
selector: 'ui-switcher',
template: `
<ng-content *ngIf="isSmall" select="mobile"></ng-content>
<ng-content *ngIf="!isSmall" select="web"></ng-content>
`
})
export class UiSwitcherComponent {
public isSmall: boolean;
constructor(breakpointObserver: BreakpointObserver) {
breakpointObserver.observe([Breakpoints.Small, Breakpoints.XSmall]).subscribe(result => {
this.isSmall = result.matches;
});
}
}
Run Code Online (Sandbox Code Playgroud)
我像这样使用它:
<ui-switcher>
<web>
<!-- some commented details -->
<input class="form-control mr-2" #searchInput
type="text" (keyup)="this.search(searchInput.value)">
</web>
<mobile>
<!-- some commented details -->
<input class="form-control" #searchInput
type="text" (keyup)="this.search(searchInput.value)">
</mobile>
</ui-switcher>
Run Code Online (Sandbox Code Playgroud)
在移动尺寸中,一切正常,但在桌面尺寸中,传递给search(value)函数的值始终为空字符串。
当我调试应用程序时,似乎#searchInputtemplateref 工作不正常(它引用的元素的值始终为空)。
为什么模板引用不能正常工作?
如果我的可观察值是false,则我应禁用下一个按钮。
可观察的是dataSource.next(如果有更多数据,则为true,否则为false)。如果没有更多数据,这将禁用按钮并阻止用户导航。
component.html:
<button mat-button (click)="next()" [disabled]="!dataSource.next$ | async">
<mat-icon>navigate_next</mat-icon>
</button>
Run Code Online (Sandbox Code Playgroud)
但是,尽管为dataSource.next输出的值是正确的,但它不能按预期工作,可能是因为如果值是false而不是true,我想禁用它吗?
例如,当没有更多数据时,dataSource.next为false,但该按钮仍处于启用状态。
解决办法是什么?