如何解决Asp.net 500 内部服务器错误和 C# Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException
当我尝试更新汽车时出现以下错误:
添加没有问题,但是更新没有完成。要更新的产品是打印控制台的时候来的,更新的时候就变了,但是后台报错,我得到了500internal服务器错误
汽车更新组件代码:
import { Component,OnInit } from '@angular/core';
import { FormBuilder,FormGroup,Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { Car } from 'src/app/models/car';
import { CarService } from 'src/app/services/car.service';
@Component({
selector: 'app-car-update',templateUrl: './car-update.component.html',styleUrls: ['./car-update.component.css']
})
export class CarUpdateComponent implements OnInit {
constructor(private carService:CarService,private formBuilder:FormBuilder,private toastrService:ToastrService,private activetedRoute:ActivatedRoute) { }
car:Car[];
carId:number;
carUpdateForm:FormGroup;
ngOnInit(): void {
this.activetedRoute.params.subscribe(params=>{
if(params["carId"]){
this.carId=params["carId"];
this.createCarUpdateForm(params["carId"]);
this.getCarDetails(params["carId"]);
}
})
}
getCarDetails(carId:number)
{
this.carService.getCarDetail(carId).subscribe(response => {
this.car = response.data;
console.log(response);
})
}
createCarUpdateForm(carId:number){
this.carUpdateForm=this.formBuilder.group({
carName:["",Validators.required],brandId:["",colorId:["",unitPrice:["",Validators.required]
})
}
update(){
if(this.carUpdateForm.valid){
let car = Object.assign({},this.carUpdateForm.value)
car.carId=this.carId;
console.log(car)
this.carService.update(car).subscribe(data=>{
this.toastrService.success(data.message,"Başarılı")
},dataError=>{
if(dataError.error.Errors.length>0){
for (let i = 0; i < dataError.error.Errors.length; i++) {
this.toastrService.error(dataError.error.Errors[i].ErrorMessage,"Doğrulama Hatası")
}
}
})
}else{
this.toastrService.error("Formunuz eksik","Dikkat")
}
}
}
汽车更新-HTML代码
<div class="content" style="width: 300px;">
<div class="col-md-12">
<div class="card">
<div class="card-header"><h5 class="title">araba Güncelle</h5></div>
<div class="card-body">
<form [formGroup]="carUpdateForm">
<div class="mb-3">
<label for="carName">araba Adı</label>
<div class="form-group">
<input type="text" id="carName" formControlName="carName" class="form-control" placeholder="carName"/> <!--ilişkilendirme formControlName ile yapılır-->
</div>
</div>
<div class="mb-3">
<label for="brandId">Marka numarası</label>
<div class="form-group">
<input type="number" id="brandId" formControlName="brandId" class="form-control" placeholder="colorId"/> <!--ilişkilendirme formControlName ile yapılır-->
</div>
</div>
<div class="mb-3">
<label for="colorId">Renk numarası</label>
<div class="form-group">
<input type="number" id="colorId" formControlName="colorId" class="form-control" placeholder="colorName"/> <!--ilişkilendirme formControlName ile yapılır-->
</div>
</div>
<div class="mb-3">
<label for="unitPrice">Birim Fiyatı</label>
<div class="form-group">
<input type="number" id="unitPrice" formControlName="unitPrice" class="form-control" placeholder="unitPrice"/> <!--ilişkilendirme formControlName ile yapılır-->
</div>
</div>
</form>
</div>
<div class="card-footer">
<button class="btn btn-fill btn-primary" (click)="update()">Güncelle</button>
</div>
</div>
</div>
</div>
汽车服务
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Car } from '../models/car';
import { ListResponseModel } from '../models/listResponseModel';
import { ResponseModel } from '../models/responseModel';
@Injectable({
providedIn: 'root'
})
export class CarService {
apiUrl = "https://localhost:44352/api/";
constructor(private httpClient: HttpClient) { }
getCars():Observable<ListResponseModel<Car>> {
let newPath = this.apiUrl + "cars/getcardetails"
return this.httpClient.get<ListResponseModel<Car>>(newPath);
}
getCarsByBrand(brandId:number):Observable<ListResponseModel<Car>> {
let newPath = this.apiUrl + "cars/getcardetailsbybrand?brandId="+brandId;
return this.httpClient.get<ListResponseModel<Car>>(newPath);
}
getCarsByColor(colorId:number):Observable<ListResponseModel<Car>>{
let newPath = this.apiUrl + "cars/getcardetailsbycolor?colorId="+colorId;
return this.httpClient.get<ListResponseModel<Car>>(newPath);
}
getCarsByColorAndBrand(colorId:number,brandId:number):Observable<ListResponseModel<Car>>{
let newPath = this.apiUrl + "cars/getcarsbycolorandbrand?brandId="+brandId+"colorId="+colorId;
return this.httpClient.get<ListResponseModel<Car>>(newPath);
}
getCarDetail(carId:number):Observable<ListResponseModel<Car>>{
let newPath = this.apiUrl + "cars/getcardetail?carId=" + carId;
return this.httpClient.get<ListResponseModel<Car>>(newPath);
}
add(car:Car):Observable<ResponseModel>{
return this.httpClient.post<ResponseModel>(this.apiUrl+"cars/add",car)
}
update(car:Car):Observable<ResponseModel>{
console.log(car)
return this.httpClient.post<ResponseModel>(this.apiUrl+"cars/update",car)
}
}
汽车控制器
[HttpPost("update")]
public IActionResult Update(Car car)
{
var result = _carService.Update(car);
if (result.Success)
{
return Ok(result);
}
return BadRequest(result.Message);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。