1

我有一些问题。这是数据库结构:

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

我做错了什么?

4

1 回答 1

0

首先,您应该始终尽量避免更改AutoDetectChangesEnabled. 当你使用它时,你真的必须彻底了解它做什么(和不做什么)。所以首先要做的就是不要碰AutoDetectChangesEnabled。看起来你的方法可以在没有它的情况下工作。如果没有,请随时告诉我们发生了什么,以便我们尝试处理它。(我怀疑会出现一些您试图通过更改来防止的问题AutoDetectChangesEnabled)。

您可以考虑使用实体框架自己的AddOrUpdate 方法。所以你在哪里

EvaluationDBContext.CriteriaHeaders.Add(OneItem);
EvaluationDBContext.Entry(OneItem).State = OneItem.CriteriaHeader_No == Guid.Empty ? EntityState.Added : EntityState.Modified;

你可以做

EvaluationDBContext.CriteriaHeaders.AddOrUpdate(OneItem);

(同样与CriteriaLines)。

但是每条记录都需要往返于数据库,以查看是否应该插入或更新。

您的代码中还有更多问题。

  • 命名约定(例如_BaseEntityClasses,约定是baseEntityClasses.
  • 您不应object[]用作输入参数,而应使用泛型:InsertOrUpdate<T>(IEnumerable<T> baseEntityClasses, ....
  • 您似乎有一些外部ErrorMessage列表,这是一种有状态的编程方式。即你的方法有副作用。可能很难说出ErrorMessage受影响的地方以及何时应该清除或报告。
  • 你吞下任何例外。这不是真正的异常处理。只需重新扔掉它们并在适当的地方处理它们。
  • 永远不要只返回一个布尔值。尤其是当你返回时,false它是没有意义的。例如,您可以返回受影响的行数。与重新抛出异常一起,消费者将从您的方法中接收到比布尔值更多的信息。
于 2013-04-14T09:50:27.550 回答