gpt4 book ai didi

entity-framework-4 - ObjectStateManager 不包含引用对象的 ObjectStateEntry

转载 作者:行者123 更新时间:2023-12-04 07:55:00 27 4
gpt4 key购买 nike

我以为我理解 EF,尤其是在极其简单的 CRUD 方面,但我可能错了。

我有一个 ObjectContext/Repository 模式,在这个例子中是一个 VatCode 实体,它有一个增值税率的集合。

增值税代码 1--* 增值税率

我想提供对这些实体进行 CRUD 的功能。这是在 WPF 应用程序中,并且正在“断开连接”,因为我没有不必要地打开数据库连接。我更喜欢数据库访问的输入/输出方法。这显示在以下集成测试中:

    [TestMethod()]
public void AddVatRateAndSaveVatCodeTest()
{
VatCode vatCode=null;
DateTime expectedDateFrom = DateTime.Now.AddDays(1).Date;
using (SopEntities sopEntities = EntitiesFactory.Create(Properties.Resources.ConnectionString))
{
VatRepository target = new VatRepository(sopEntities);
vatCode = target.SingleOrDefault(q => q.Key == "X4");
Assert.IsNotNull(vatCode);
}

// mimick offline editing/disconnect

using (SopEntities sopEntities = EntitiesFactory.Create(Properties.Resources.ConnectionString))
{
VatRepository target = new VatRepository(sopEntities);


vatCode.VatRates.Add(new VatRate()
{
DateFrom=expectedDateFrom,
Rate=20
});
target.Attach(vatCode);
target.SaveChanges();
}

using (SopEntities sopEntities=EntitiesFactory.Create(Properties.Resources.ConnectionString))
{
VatRepository vatRepository=new VatRepository(sopEntities);
VatCode actual=vatRepository.SingleOrDefault(q=>q.Key=="X3");
Assert.IsNotNull(actual);
Assert.AreEqual(vatCode.VatRates.Count,actual.VatRates.Count);
Assert.AreEqual(expectedDateFrom,actual.VatRates.OrderBy(q=>q.DateFrom).Last().DateFrom);
}
}

代码很简单。它检索已知的现有实体并将其分配给增值税代码。然后,该实体在 UI 上的 ViewModel 中传递,并重新保存。在这个测试中,我通过打开一个新的上下文来模拟这个。这正是我在 UI 中的表现,所以我在这里证明了这一点。

但是,在调用 vatRepository 的 Attach() 方法时,它具有以下方法主体:
    public void Attach(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");

_context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); // exception here
_objectSet.Attach(entity);
}

我在 ChangeObjectState 方法上得到以下异常(我调用该方法是因为实体与原​​始上下文范围断开连接)。

System.InvalidOperationException was unhandled by user code
Message=The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'Mac.Sop.Core.Data.VatCode'.



我确实认为 EF 模型中的 XML 可能不正确(考虑对“Mac.Sop.Core.Data.VatCode”的引用),但查看它我找不到任何问题,这些实体与我的其他实体遵循类似的模式.

更新:
我的EF模型。我找不到任何对“Mac.Sop.Core.Data.VatCode”的引用:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Runtime>
<edmx:StorageModels>
<Schema Namespace="Model.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="ModelStoreContainer">
<EntitySet Name="MacsSop_VATCode" EntityType="Model.Store.MacsSop_VATCode" store:Type="Tables" Schema="dbo" />
<EntitySet Name="MacsSop_VATRate" EntityType="Model.Store.MacsSop_VATRate" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_MacsSop_VATRate_MacsSop_VATCode" Association="Model.Store.FK_MacsSop_VATRate_MacsSop_VATCode">
<End Role="MacsSop_VATCode" EntitySet="MacsSop_VATCode" />
<End Role="MacsSop_VATRate" EntitySet="MacsSop_VATRate" />
</AssociationSet>
</EntityContainer>
<EntityType Name="MacsSop_VATCode">
<Key>
<PropertyRef Name="Key" />
</Key>
<Property Name="Key" Type="varchar" Nullable="false" MaxLength="3" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="128" />
</EntityType>
<EntityType Name="MacsSop_VATRate">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Key" Type="varchar" Nullable="false" MaxLength="3" />
<Property Name="DateFrom" Type="datetime" Nullable="false" />
<Property Name="Rate" Type="decimal" Nullable="false" Scale="2" />
</EntityType>
<Association Name="FK_MacsSop_VATRate_MacsSop_VATCode">
<End Role="MacsSop_VATCode" Type="Model.Store.MacsSop_VATCode" Multiplicity="1" />
<End Role="MacsSop_VATRate" Type="Model.Store.MacsSop_VATRate" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="MacsSop_VATCode">
<PropertyRef Name="Key" />
</Principal>
<Dependent Role="MacsSop_VATRate">
<PropertyRef Name="Key" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="SopEntities" annotation:LazyLoadingEnabled="true" >
<EntitySet Name="VatCodes" EntityType="Model.VatCode" />
<EntitySet Name="VatRates" EntityType="Model.VatRate" />
<AssociationSet Name="FK_MacsSop_VATRate_MacsSop_VATCode" Association="Model.FK_MacsSop_VATRate_MacsSop_VATCode">
<End Role="MacsSop_VATCode" EntitySet="VatCodes" />
<End Role="MacsSop_VATRate" EntitySet="VatRates" />
</AssociationSet>
</EntityContainer>
<EntityType Name="VatCode">
<Key>
<PropertyRef Name="Key" />
</Key>
<Property Type="String" Name="Key" Nullable="false" MaxLength="3" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Name" Nullable="false" MaxLength="128" FixedLength="false" Unicode="true" />
<NavigationProperty Name="VatRates" Relationship="Model.FK_MacsSop_VATRate_MacsSop_VATCode" FromRole="MacsSop_VATCode" ToRole="MacsSop_VATRate" />
</EntityType>
<EntityType Name="VatRate">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Type="Int32" Name="ID" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Key" Nullable="false" MaxLength="3" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="DateFrom" Nullable="false" />
<Property Type="Decimal" Name="Rate" Nullable="false" Precision="18" Scale="2" />
<NavigationProperty Name="VatCode" Relationship="Model.FK_MacsSop_VATRate_MacsSop_VATCode" FromRole="MacsSop_VATRate" ToRole="MacsSop_VATCode" />
</EntityType>
<Association Name="FK_MacsSop_VATRate_MacsSop_VATCode">
<End Type="Model.VatCode" Role="MacsSop_VATCode" Multiplicity="1" />
<End Type="Model.VatRate" Role="MacsSop_VATRate" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="MacsSop_VATCode">
<PropertyRef Name="Key" />
</Principal>
<Dependent Role="MacsSop_VATRate">
<PropertyRef Name="Key" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="ModelStoreContainer" CdmEntityContainer="SopEntities" >
<EntitySetMapping Name="VatCodes">
<EntityTypeMapping TypeName="Model.VatCode">
<MappingFragment StoreEntitySet="MacsSop_VATCode">
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Key" ColumnName="Key" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="VatRates">
<EntityTypeMapping TypeName="Model.VatRate">
<MappingFragment StoreEntitySet="MacsSop_VATRate">
<ScalarProperty Name="Rate" ColumnName="Rate" />
<ScalarProperty Name="DateFrom" ColumnName="DateFrom" />
<ScalarProperty Name="Key" ColumnName="Key" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>

</edmx:Edmx>

这需要以这种断开连接的方式工作,并且必须是可能的,因为这种模式的 WCF/ASP.NET 实现显然存在!

最佳答案

交换 AttachChangeObjectState行,因为您需要将实体添加到 ObjectStateManager以改变状态。

public void Attach(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity");

_objectSet.Attach(entity);
_context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}

关于entity-framework-4 - ObjectStateManager 不包含引用对象的 ObjectStateEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10432983/

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