我有一些问题。这是数据库结构:
CREATE TABLE [EvaluationProcess].[CriteriaHeader](
[CriteriaHeader_No] [uniqueidentifier] NOT NULL,
[CriteriaHeader_Type] [int] NOT NULL,
[CriteriaHeader_Name] [nvarchar](128) NOT NULL,
[CriteriaHeader_Description] [nvarchar](256) NULL,
[CriteriaHeader_MaxScore] [int] NOT NULL,
[CriteriaHeader_MinScore] [int] NOT NULL,
[CriteriaHeader_ScoreStep] [int] NOT NULL,
[CriteriaHeader_IsCountedResult] [bit] NOT NULL,
[CriteriaHeader_IsBlocked] [bit] NOT NULL,
[OwnedOrganisationID] [uniqueidentifier] NULL,
[ModifyUser] [uniqueidentifier] NULL,
[ModifyDate] [datetime] NULL,
[TimeStamp] [timestamp] NULL,
CONSTRAINT [PK_Criteria] PRIMARY KEY CLUSTERED
(
[CriteriaHeader_No] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_No] DEFAULT (newsequentialid()) FOR [CriteriaHeader_No]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_Type] DEFAULT ((0)) FOR [CriteriaHeader_Type]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_MaxScore_1] DEFAULT ((5)) FOR [CriteriaHeader_MaxScore]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_MinScore_1] DEFAULT ((0)) FOR [CriteriaHeader_MinScore]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_ScoreStep_1] DEFAULT ((1)) FOR [CriteriaHeader_ScoreStep]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_IsCountedResult] DEFAULT ((0)) FOR [CriteriaHeader_IsCountedResult]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_Criteria_IsBlocked] DEFAULT ((0)) FOR [CriteriaHeader_IsBlocked]
GO
ALTER TABLE [EvaluationProcess].[CriteriaHeader] ADD CONSTRAINT [DF_Criteria_ModifyDate] DEFAULT (getdate()) FOR [ModifyDate]
GO
CREATE TABLE [EvaluationProcess].[CriteriaLine](
[CriteriaLine_No] [uniqueidentifier] NOT NULL,
[CriteriaLine_ParentID] [uniqueidentifier] NULL,
[CriteriaLine_CriteriaHeaderID] [uniqueidentifier] NULL,
[CriteriaLine_Text] [nvarchar](128) NOT NULL,
[CriteriaLine_Description] [nvarchar](512) NULL,
[OwnedOrganisationID] [uniqueidentifier] NULL,
[ModifyUser] [uniqueidentifier] NULL,
[ModifyDate] [datetime] NULL,
[TimeStamp] [timestamp] NULL,
CONSTRAINT [PK_CriteriaList] PRIMARY KEY CLUSTERED
(
[CriteriaLine_No] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] ADD CONSTRAINT [DF_CriteriaList_CriteriaList_No] DEFAULT (newsequentialid()) FOR [CriteriaLine_No]
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] ADD CONSTRAINT [DF_CriteriaList_ModifyDate] DEFAULT (getdate()) FOR [ModifyDate]
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] WITH CHECK ADD CONSTRAINT [FK_CriteriaLine_CriteriaLine] FOREIGN KEY([CriteriaLine_CriteriaHeaderID])
REFERENCES [EvaluationProcess].[CriteriaHeader] ([CriteriaHeader_No])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] CHECK CONSTRAINT [FK_CriteriaLine_CriteriaLine]
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] WITH CHECK ADD CONSTRAINT [FK_CriteriaLine_CriteriaLine1] FOREIGN KEY([CriteriaLine_ParentID])
REFERENCES [EvaluationProcess].[CriteriaLine] ([CriteriaLine_No])
GO
ALTER TABLE [EvaluationProcess].[CriteriaLine] CHECK CONSTRAINT [FK_CriteriaLine_CriteriaLine1]
GO
我使用 EntityFramework 5。这是一个添加/更新函数
public override bool InsertOrUpdate(object[] _BaseEntityClasses, string _RoleName,
System.Nullable<Guid> _LoginUserID,
System.Nullable<System.Guid> _OrganisationID, bool? IsModified = null)
{
using (Evaluation_SystemConection EvaluationDBContext = new Evaluation_SystemConection())
{
try
{
EvaluationDBContext.Configuration.AutoDetectChangesEnabled = false;
IEnumerable<CriteriaHeader> _BaseEntityClassesList = _BaseEntityClasses.Cast<CriteriaHeader>();
foreach (var OneItem in _BaseEntityClassesList)
{
if (OneItem.CriteriaLines != null)
{
foreach (var OneLine in OneItem.CriteriaLines)
{
if (OneLine.CriteriaLine_No == Guid.Empty)
OneLine.CriteriaLine_CriteriaHeaderID = OneItem.CriteriaHeader_No;
EvaluationDBContext.CriteriaLines.Attach(OneLine);
EvaluationDBContext.Entry(OneLine).State = OneLine.CriteriaLine_No == Guid.Empty ? EntityState.Added : EntityState.Modified;
}
}
EvaluationDBContext.CriteriaHeaders.Add(OneItem);
EvaluationDBContext.Entry(OneItem).State = OneItem.CriteriaHeader_No == Guid.Empty ? EntityState.Added : EntityState.Modified;
if (EvaluationDBContext.SaveChanges() <= 0)
return false;
}
return true;
}
catch (Exception Ex)
{
ErrorMessage.Add("");
ErrorMessage.Add((Ex.InnerException != null) ? Ex.InnerException.Message : Ex.Message);
return false;
}
finally
{
EvaluationDBContext.Configuration.AutoDetectChangesEnabled = true;
}
}
}
并生成 POCO 类
public partial class CriteriaHeader
{
public CriteriaHeader()
{
this.CriteriaLines = new HashSet<CriteriaLine>();
}
public System.Guid CriteriaHeader_No { get; set; }
public int CriteriaHeader_Type { get; set; }
public string CriteriaHeader_Name { get; set; }
public string CriteriaHeader_Description { get; set; }
public int CriteriaHeader_MaxScore { get; set; }
public int CriteriaHeader_MinScore { get; set; }
public int CriteriaHeader_ScoreStep { get; set; }
public bool CriteriaHeader_IsCountedResult { get; set; }
public bool CriteriaHeader_IsBlocked { get; set; }
public Nullable<System.Guid> OwnedOrganisationID { get; set; }
public Nullable<System.Guid> ModifyUser { get; set; }
public Nullable<System.DateTime> ModifyDate { get; set; }
public byte[] TimeStamp { get; set; }
public virtual ICollection<CriteriaLine> CriteriaLines { get; set; }
}
public partial class CriteriaLine
{
public CriteriaLine()
{
this.CriteriaLine1 = new HashSet<CriteriaLine>();
}
public System.Guid CriteriaLine_No { get; set; }
public Nullable<System.Guid> CriteriaLine_ParentID { get; set; }
public Nullable<System.Guid> CriteriaLine_CriteriaHeaderID { get; set; }
public string CriteriaLine_Text { get; set; }
public string CriteriaLine_Description { get; set; }
public Nullable<System.Guid> OwnedOrganisationID { get; set; }
public Nullable<System.Guid> ModifyUser { get; set; }
public Nullable<System.DateTime> ModifyDate { get; set; }
public byte[] TimeStamp { get; set; }
public virtual CriteriaHeader CriteriaHeader { get; set; }
public virtual ICollection<CriteriaLine> CriteriaLine1 { get; set; }
public virtual CriteriaLine CriteriaLine2 { get; set; }
}
数据未在 中插入或更新超过 1 个级别CriteriaList
。
我做错了什么?