我在WCF项目中拥有此模型,并使用Entity Framework 6作为其ORM。
[DataContract(IsReference = true)]
public class ProductSerialInfo
{
[DataMember]
public int WorkOrderNo { get; set; }
[DataMember]
public decimal ProductMainSerial { get; set; }
[DataMember]
public string ProductCode { get; set; }
[DataMember]
public int GroupId { get; set; }
[DataMember]
public int TestIndex { get; set; }
[DataMember]
public BindingList<ProductSerialInfoControlSupervisor> ControlSupervisorList { get; set; }
[DataMember]
public BindingList<ProductFault> ProductFaultList { get; set; }
}
[DataContract(IsReference = true)]
public class ProductSerialInfoControlSupervisor
{
[DataMember]
public int WorkOrderNo { get; set; }
[DataMember]
public decimal ProductMainSerial { get; set; }
[DataMember]
public string ProductCode { get; set; }
[DataMember]
public int TestIndex { get; set; }
[DataMember]
public int ControlSupervisorId { get; set; }
[DataMember]
public int CreateUserId { get; set; }
[DataMember]
public int ChangeUserId { get; set; }
[DataMember]
public DateTime CreateDate { get; set; }
[DataMember]
public DateTime ChangeDate { get; set; }
[DataMember]
public byte[] RowVersion { get; set; }
[DataMember]
public virtual ProductSerialInfo ProductSerialInfo { get; set; }
[DataMember]
public virtual ProductSupervisor ProductSupervisor { get; set; }
}
[DataContract(IsReference = true)]
public class ProductFault
{
[DataMember]
public int WorkOrderNo { get; set; }
[DataMember]
public decimal ProductMainSerial { get; set; }
[DataMember]
public string ProductCode { get; set; }
[DataMember]
public int TestIndex { get; set; }
[DataMember]
public int FaultId { get; set; }
[DataMember]
public int FaultCount { get; set; }
[DataMember]
public string FaultComment { get; set; }
[DataMember]
public virtual ProductSerialInfo FinalProductFault { get; set; }
}
当我尝试删除ProductSerialInfo实例类型的对象时,我得到
The relationship could not be changed because one or more of the foreign-key properties is non-nullable
例外。我的删除方法如下:
public virtual void Delete(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != EntityState.Deleted)
{
dbEntityEntry.State = EntityState.Deleted;
}
else
{
DbSet.Attach(entity);
DbSet.Remove(entity);
}
}
这是通用的。 其用法如下:
public int SaveProductSerialInfo(List<ProductSerialInfo> productSerialInfo)
{
try
{
int workOrderNo = productSerialInfo.ElementAt(0).WorkOrderNo;
string productCode = productSerialInfo.ElementAt(0).ProductCode;
decimal productMainSerial = productSerialInfo.ElementAt(0).ProductMainSerial;
int testIndex = productSerialInfo.ElementAt(0).TestIndex;
ProductSerialInfo itemToManage = Uow.ProductSerialInfo.GetAllByCondition(x => x.ProductCode.Trim() == productCode && x.TestIndex == testIndex && x.ProductMainSerial == productMainSerial && x.WorkOrderNo == workOrderNo).FirstOrDefault();
if (itemToManage != null)
{
Uow.ProductSerialInfo.Delete(itemToManage);
Uow.Commit();
}
foreach (var item in productSerialInfo)
{
Uow.ProductSerialInfo.Add(item);
Uow.Commit();
}
}
catch (Exception ex)
{
return 0;
}
return 1;
}
ProductSerialInfo类的配置设置为在删除时级联,这意味着它应该从数据库中删除相关的ProductSerialInfoControlSupervisor和ProductFault实例。
public class ProductSerialInfoConfiguration : EntityTypeConfiguration<ProductSerialInfo>
{
public ProductSerialInfoConfiguration()
{
HasKey(x => new { x.ProductCode, x.WorkOrderNo, x.ProductMainSerial, x.TestIndex});
Property(t => t.GroupId)
.IsRequired()
.HasColumnName("GroupId");
Property(t => t.ProductCode)
.IsRequired()
.HasColumnName("ProductCode");
Property(t => t.TestIndex)
.IsRequired()
.HasColumnName("TestIndex");
HasMany(x => x.ProductFaultList)
.WithRequired(x => x.FinalProductFault)
.HasForeignKey(x => new { x.ProductCode, x.WorkOrderNo, x.ProductMainSerial, x.TestIndex })
.WillCascadeOnDelete(true);
HasMany(x => x.ControlSupervisorList)
.WithRequired(x => x.ProductSerialInfo)
.HasForeignKey(x => new { x.ProductCode, x.WorkOrderNo, x.ProductMainSerial, x.TestIndex })
.WillCascadeOnDelete(true);
}
}