【发布时间】:2021-03-26 17:19:09
【问题描述】:
if (tubeCodes.Count > 0)
{
foreach (var item in tubeCodes)
{
PrepLabSampleTubeVol entity = new PrepLabSampleTubeVol()
{
SampleId = sample.SampleId,
TubeCode = item,
Volume = sample.Vol==null ? 0:sample.Vol,
VersionNo = 0,
TransactionID = Guid.NewGuid(),
CreatedBy = CurrentUserInfo.LabName,
CreatedDttm = DateTime.Now,
UpdatedBy = CurrentUserInfo.LabName,
UpdatedDttm = DateTime.Now,
};
tubeVolumnList.Add(entity);
}
}
using (var uow = new UnitOfWork(db))
{
tubeVolumnList.ForEach(x => uow.Repository<PrepLabSampleTubeVol>().Insert(x));
//uow.Repository<PrepLabSampleTubeVol>().BulkInsert(tubeVolumnList);
uow.Repository<PrepLabSample>().Update(sample);
uow.Save();
return true;
}
当我为每个项目生成TransactionID 时,它们彼此不同,但是当代码去插入时,所有项目的TransactionID 都变成了相同的。在数据库中,TransactionID是表的主键。
public partial class PrepLabSampleTubeVol:EntityBase,GWZ.Lab.Entity.IAuditableEntity
{
public Nullable<System.Guid> SampleId { get; set; }
public string TubeCode { get; set; }
public Nullable<decimal> Volume { get; set; }
public int VersionNo { get; set; }
[Key]
public System.Guid TransactionID { get; set; }
public string CreatedBy { get; set; }
public System.DateTime CreatedDttm { get; set; }
public string UpdatedBy { get; set; }
public System.DateTime UpdatedDttm { get; set; }
}
异常信息:
快速观看:
【问题讨论】:
-
EF 可能假设数据库将生成新的 GUID?你能发布类和模型构建定义吗?
-
更新了,请看。
-
是的,对于 Guid & int 键的 EF 将假定 db 将生成一个值。 (EF 核心还是 EF 6??)docs.microsoft.com/en-us/ef/core/modeling/…
标签: c# .net asp.net-mvc entity-framework linq