gpt4 book ai didi

entity-framework - 关于 System.Data.EntityState.Add 和 DbSet.Add 之间差异(如果有)的令人困惑的文章和文档

转载 作者:行者123 更新时间:2023-12-04 07:24:54 25 4
gpt4 key购买 nike

我正在使用 EF 5 开发 C# ASP.NET MVC 5 Web 应用程序。使用 EF 映射我的数据库表会生成 DbContext类和 .edmx文件。今天,我正在阅读a great article about creating generic DAL classes ,但我停在了以下句子上:

Note that using the Entry method to change the state of an entity will only affect the actual entity that you pass in to the method. It won’t cascade through a graph and set the state of all related objects, unlike the DbSet.Add method.



这与这些问题中提到的内容相矛盾:
  • http://forums.asp.net/p/2015170/5803192.aspx
  • http://forums.asp.net/p/2060606/5943259.aspx
  • Difference between DbSet.Add(entity) and entity.State = EntityState.Added
  • What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

  • 在以上所有问题的回答中,所有用户都提到使用 System.Data.EntityState.Added与使用 DbSet.Add 完全相同.但是我首先提到的文章指出使用 System.Data.EntityState.Added不会通过图形级联。

    根据我的测试,我得出结论,使用 System.Data.EntityState.Added将级联通过与 DBset.Add 相同的图形案件。是文章错了,还是我的测试和问答?

    最佳答案

    这些方法是相同的,您可以通过常规测试进行验证,或者,如果您想完全确定 - 通过对 EF 6 代码的一些探索。

  • DbSet.Add方法 ( http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/DbSet.cs )
    public virtual TEntity Add(TEntity entity)
    {
    Check.NotNull<TEntity>(entity, "entity");
    this.GetInternalSetWithCheck("Add").Add((object) entity);
    return entity;
    }

  • 这叫 InternalSet<T>.Add(object)方法。
  • DbEntityEntry<T>.State属性(property) ( http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Infrastructure/DbEntityEntry.cs )
    public EntityState State
    {
    get { return _internalEntityEntry.State; }
    set { _internalEntityEntry.State = value; }
    }

  • 哪里 _internalEntityEntryInternalEntityEntry类型。
    InternalEntityEntry.State属性(property) ( http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Internal/EntityEntries/InternalEntityEntry.cs )
        public virtual EntityState State
    {
    get { return IsDetached ? EntityState.Detached : _stateEntry.State; }
    set
    {
    if (!IsDetached)
    {
    if (_stateEntry.State == EntityState.Modified
    && value == EntityState.Unchanged)
    {
    // Special case modified to unchanged to be "reject changes" even
    // ChangeState will do "accept changes". This keeps the behavior consistent with
    // setting modified to false at the property level (once that is supported).
    CurrentValues.SetValues(OriginalValues);
    }
    _stateEntry.ChangeState(value);
    }
    else
    {
    switch (value)
    {
    case EntityState.Added:
    _internalContext.Set(_entityType).InternalSet.Add(_entity);
    break;
    case EntityState.Unchanged:
    _internalContext.Set(_entityType).InternalSet.Attach(_entity);
    break;
    case EntityState.Modified:
    case EntityState.Deleted:
    _internalContext.Set(_entityType).InternalSet.Attach(_entity);
    _stateEntry = _internalContext.GetStateEntry(_entity);
    Debug.Assert(_stateEntry != null, "_stateEntry should not be null after Attach.");
    _stateEntry.ChangeState(value);
    break;
    }
    }
    }
    }

    您会看到,如果实体已分离(您的情况)并且状态已添加 - 相同 InternalSet<T>.Add(object)叫做。

    至于通过测试验证:
    using (var ctx = new TestDBEntities()) {
    // just some entity, details does not matter
    var code = new Code();
    // another entity
    var error = new Error();
    // Code has a collection of Errors
    code.Errors.Add(error);
    var codeEntry = ctx.Entry(code);
    // modify code entry and mark as added
    codeEntry.State = EntityState.Added;
    // note we did not do anything with Error
    var errorEntry = ctx.Entry(error);
    // but it is marked as Added too, because when marking Code as Added -
    // navigation properties were also explored and attached, just like when
    // you do DbSet.Add
    Debug.Assert(errorEntry.State == EntityState.Added);
    }

    关于entity-framework - 关于 System.Data.EntityState.Add 和 DbSet.Add 之间差异(如果有)的令人困惑的文章和文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36652888/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com