我有一个从IEnumerable转换的List,我试图将它转储到我的数据库中。对于这个例子,我有12条记录,我试图使用foreach来处理所有记录。问题在于,当添加记录时,当我在循环中使用SaveChanges()
方法时,我得到主键违规,当它在循环外侧时,我得到一个全零的Guid。
我的代码是:
public UnitsOfMeasureDataWithMessage UnitOfMeasureRelatedUnits_Create(IEnumerable<UnitOfMeasureRelatedUnitDataInbound> _UnitOfMeasureRelatedUnitDataInbound)
{
UnitOfMeasureRelatedUnitDataInbound _newUnitOfMeasureRelatedUnitDataInbound = new UnitOfMeasureRelatedUnitDataInbound();
UnitsOfMeasureDataWithMessage _oUnitsOfMeasureDataWithMessage = new UnitsOfMeasureDataWithMessage();
UnitOfMeasureSetRelatedUnit _oUnitOfMeasureSetRelatedUnit = new UnitOfMeasureSetRelatedUnit();
List<UnitOfMeasureRelatedUnitDataInbound> _listUnitOfMeasureRelatedUnitDataInbound = _UnitOfMeasureRelatedUnitDataInbound.ToList();
foreach (UnitOfMeasureRelatedUnitDataInbound _RelatedUnit in _listUnitOfMeasureRelatedUnitDataInbound)
{
_newUnitOfMeasureRelatedUnitDataInbound.Active = _oUnitOfMeasureSetRelatedUnit.Active = _RelatedUnit.Active;
_newUnitOfMeasureRelatedUnitDataInbound.BaseUnitAbbreviation = _oUnitOfMeasureSetRelatedUnit.BaseUnitAbbreviation = _RelatedUnit.BaseUnitAbbreviation;
_newUnitOfMeasureRelatedUnitDataInbound.BaseUnitID = _oUnitOfMeasureSetRelatedUnit.BaseUnitID = _RelatedUnit.BaseUnitID;
_newUnitOfMeasureRelatedUnitDataInbound.BaseUnitName = _oUnitOfMeasureSetRelatedUnit.BaseUnitName = _RelatedUnit.BaseUnitName;
_newUnitOfMeasureRelatedUnitDataInbound.Name = _oUnitOfMeasureSetRelatedUnit.Name = _RelatedUnit.Name;
_newUnitOfMeasureRelatedUnitDataInbound.RelatedUnitAbbreviation = _oUnitOfMeasureSetRelatedUnit.RelatedUnitAbbreviation = _RelatedUnit.RelatedUnitAbbreviation;
_newUnitOfMeasureRelatedUnitDataInbound.RelatedUnitConversionRatio = _oUnitOfMeasureSetRelatedUnit.RelatedUnitConversionRatio = _RelatedUnit.RelatedUnitConversionRatio;
_newUnitOfMeasureRelatedUnitDataInbound.RelatedUnitDisplayOrder = _oUnitOfMeasureSetRelatedUnit.RelatedUnitDisplayOrder = _RelatedUnit.RelatedUnitDisplayOrder;
_newUnitOfMeasureRelatedUnitDataInbound.RelatedUnitName = _oUnitOfMeasureSetRelatedUnit.RelatedUnitName = _RelatedUnit.RelatedUnitName;
_newUnitOfMeasureRelatedUnitDataInbound.UnitOfMeasureSetID = _oUnitOfMeasureSetRelatedUnit.UnitOfMeasureID = _RelatedUnit.UnitOfMeasureSetID;
_newUnitOfMeasureRelatedUnitDataInbound.UnitOfMeasureTypeID = _oUnitOfMeasureSetRelatedUnit.UnitOfMeasureSetID = _RelatedUnit.UnitOfMeasureTypeID;
_oDBContext.UnitOfMeasureSetRelatedUnits.Add(_oUnitOfMeasureSetRelatedUnit);
}
_oDBContext.SaveChanges();
return _oUnitsOfMeasureDataWithMessage;
}
我的SQL架构是:
[ID] [UNIQUEIDENTIFIER] NOT NULL,
[Name] [NVARCHAR](50) NULL,
[Active] [BIT] NULL,
[UnitOfMeasureID] [UNIQUEIDENTIFIER] NULL,
[BaseUnitID] [UNIQUEIDENTIFIER] NULL,
[BaseUnitName] [NVARCHAR](50) NULL,
[BaseUnitAbbreviation] [NVARCHAR](50) NULL,
[RelatedUnitDisplayOrder] [INT] NULL,
[RelatedUnitName] [NVARCHAR](50) NULL,
[RelatedUnitAbbreviation] [NVARCHAR](50) NULL,
[RelatedUnitConversionRatio] [FLOAT] NULL,
[UnitOfMeasureSetID] [UNIQUEIDENTIFIER] NULL,
答案 0 :(得分:0)
EF专门处理自动增量整数标识列。它知道它不应该尝试在其中插入0或NULL。它不以相同的方式尊重默认值。无论您将它作为主键的值,它都会尝试插入表中。如果您的ID为Guid.Empty
,则会尝试使用Guid.Empty
插入12条记录,并且您将收到您所遇到的错误。
解决方案是在构造函数中将ID设置为Guid.NewGuid
。这与列的默认值是多余的,但它不会产生任何问题 - 无论您是在EF还是在数据库中创建新的guid,都只是创建一个新的guid。
这是您应该对EF中的所有默认值执行的操作。优点是,假设您有一个位列,默认值为1.如果您在代码中创建一个新实体,那么您希望该布尔属性的值在创建后立即得到什么? true
,对吧?所以你必须在构造函数中这样做。如果只使用EF插入数据库,那么实际上数据库列的默认值是冗余的,而不是构造函数中的默认设置。如果有时使用其他方法插入数据库,则EF和数据库都需要单独强制执行默认值。然后,新创建的实体和新创建的数据库行都将具有相同的行为。
答案 1 :(得分:0)
要解决此问题,请打开.EDMX文件,在相关表格中选择PK,在F4中搜索属性,然后将StoreGeneratedPattern
值更改为Identity
。