【发布时间】:2017-09-25 15:25:39
【问题描述】:
我的 WebApi 应用程序有一个小问题。 当我将新实体添加到数据库中时会出现此问题。
实体:
public sealed class Lot : Entity
{
[Required, DefaultValue(false)]
public bool IsActive { get; set; }
[Required, DefaultValue(false)]
public bool IsDelete { get; set; }
[Required, DefaultValue(false)]
public bool InCredit { get; set; }
[DefaultValue(1), Range(1, 10)]
public int PhaseNumber { get; set; }
[Required]
public decimal DesiredPrice { get; set; }
public Guid? AcceptedBetId { get; set; }
[Required]
public DateTime CreationDate { get; set; }
public DateTime ExpirationDateTime { get; set; }
[Required]
public string City { get; set; }
public User User { get; set; }
public Car Car { get; set; }
public ICollection<Bet> Bets { get; set; }
public Lot()
{
Bets = new List<Bet>();
}
}
public sealed class Bet : Entity
{
[Required, Range(0.0, double.MaxValue)]
public decimal Price { get; set; }
[Required]
public DateTime CreationDate { get; set; }
[Range(0, int.MaxValue)]
public int BetNumber { get; set; }
public Lot Lot { get; set; }
public User User { get; set; }
}
我想在我的数据库中添加新的 Bet 实体的代码:
Lot lot;
using (LotsManager lotsManager = new LotsManager())
{
lot = await lotsManager.GetLotAsync(model.LotId);
}
User user;
using (UserManager userManager = new UserManager())
{
user = await userManager.GetUserAsync(model.UserInfoId);
}
var bet = new Bet
{
Id = Guid.NewGuid(),
Price = model.Price,
CreationDate = DateTime.Now,
BetNumber = lastBetNumber + 1,
Lot = lot,
User = user
};
await _betsManager.SaveBetAsync(bet);
在这个地方将我的实体添加到数据库中(数据库:我的 SQL)
protected async Task Save<T>(T entity) where T : Entity
{
using (var dbContextTransaction = _context.Database.BeginTransaction())
{
try
{
var dbEntity = await _context.Set<T>().SingleOrDefaultAsync(x => x.Id == entity.Id);
_context.Set<T>().Attach(entity);
_context.Entry(entity).State = dbEntity != null ? EntityState.Modified : EntityState.Added;
await _context.SaveChangesAsync();
dbContextTransaction?.Commit();
}
catch (DbEntityValidationException ex)
{
dbContextTransaction?.Rollback();
var error = ex.EntityValidationErrors.First().ValidationErrors.First();
throw new InvalidModelException(error.ErrorMessage);
}
catch (Exception ex)
{
dbContextTransaction?.Rollback();
throw new InvalidDbOperationException(ex.Message);
}
}
}
当我调用我的保存方法时,我的代码抛出了这个异常:
附加类型为“WebCar.Domain.Entities.Lot”的实体失败,因为同一类型的另一个实体已经具有相同的主键值。如果图中的任何实体具有冲突的键值,则在使用“附加”方法或将实体的状态设置为“未更改”或“已修改”时,可能会发生这种情况。这可能是因为某些实体是新实体,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“已添加”实体状态来跟踪图形,然后将非新实体的状态设置为“未更改”或“已修改”。
我正在尝试使用
_context.Set<T>().Attach(entity);
_context.Entry(entity).State = EntityState.Unchanged;
or
_context.Entry(entity).State = EntityState.Unchanged;
但是代码还是会报错。
【问题讨论】:
标签: c# entity-framework asp.net-web-api entity-framework-5