0

我有两个具有:n 关系的实体。让我们说“人员和地址”。如果我向 person.addresses 添加地址,则人员的 CodeFluentEntityState 不会更改。在 person 的 baseSave 中有一个函数 SaveAddressesRelations,它通常会保存添加的地址。但是,由于 person 的 CodeFluentEntityState 没有更改,因此无法访问此代码并且不会保存地址。

请修复它,该错误导致我的应用程序出现大问题..

我做了一个小项目来澄清:

<cf:project defaultNamespace="test" xmlns:cf="http://www.softfluent.com/codefluent/2005/1" xmlns:cfx="http://www.softfluent.com/codefluent/modeler/2008/1" xmlns:cfom="http://www.softfluent.com/codefluent/producers.model/2005/1" createDefaultMethodForms="true" createDefaultApplication="false" createDefaultHints="false">
  <cf:import path="Default.Surface.cfp" />
  <cf:entity name="Person" namespace="GreatPersons" categoryPath="/test">
    <cf:property name="Guid" key="true" />
    <cf:property name="Name" />
    <cf:property name="Addresses" typeName="GreatPersons.AddressCollection" relationPropertyName="Persons" />
  </cf:entity>
  <cf:entity name="Address" namespace="GreatPersons" categoryPath="/test">
    <cf:property name="Guid" key="true" />
    <cf:property name="Street" />
    <cf:property name="Persons" typeName="GreatPersons.PersonCollection" relationPropertyName="Addresses" />
  </cf:entity>
  <cf:producer name="Business Object Model (BOM)" typeName="CodeFluent.Producers.CodeDom.CodeDomProducer, CodeFluent.Producers.CodeDom">
    <cf:configuration compileWithVisualStudio="true" compile="false" codeDomProviderTypeName="CSharp" targetDirectory="..\Test\CodefluentCode" cfx:targetProjectLayout="Update" cfx:targetProject="..\Test\Test.csproj" />
  </cf:producer>
</cf:project>

以下代码不会保存人与地址之间的关系:

var newPerson = new Person
{
    Name = "Napoleon"
};
newPerson.Save();

var newAddress = new Address
{
    Street = "Saint Helena Road"
};
newAddress.Save();

newPerson.Addresses.Add(newAddress);
newPerson.Save();

BOM生产者生成的人的save方法如下:

protected virtual bool BaseSave(bool force)
{
    if ((this.EntityState == CodeFluent.Runtime.CodeFluentEntityState.ToBeDeleted))
    {
        this.Delete();
        return false;
    }
    CodeFluent.Runtime.CodeFluentEntityActionEventArgs evt = new CodeFluent.Runtime.CodeFluentEntityActionEventArgs(this, CodeFluent.Runtime.CodeFluentEntityAction.Saving, true);
    this.OnEntityAction(evt);
    if ((evt.Cancel == true))
    {
        return false;
    }
    CodeFluentPersistence.ThrowIfDeleted(this);
    this.Validate();
    if (((force == false) 
                && (this.EntityState == CodeFluent.Runtime.CodeFluentEntityState.Unchanged)))
    {
        return false;
    }
    CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(test.Constants.testStoreName).Persistence;
    persistence.CreateStoredProcedureCommand(null, "Person", "Save");
    persistence.AddParameter("@Person_Guid", this.Guid, CodeFluentPersistence.DefaultGuidValue);
    persistence.AddParameter("@Person_Name", this.Name, default(string));
    persistence.AddParameter("@_trackLastWriteUser", persistence.Context.User.Name);
    persistence.AddParameter("@_rowVersion", this.RowVersion);
    System.Data.IDataReader reader = null;
    try
    {
        reader = persistence.ExecuteReader();
        if ((reader.Read() == true))
        {
            this.ReadRecordOnSave(reader);
        }
        CodeFluentPersistence.NextResults(reader);
    }
    finally
    {
        if ((reader != null))
        {
            reader.Dispose();
        }
        persistence.CompleteCommand();
    }
    this.SaveAddressesRelations();
    this.OnEntityAction(new CodeFluent.Runtime.CodeFluentEntityActionEventArgs(this, CodeFluent.Runtime.CodeFluentEntityAction.Saved, false, false));
    this.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Unchanged;
    return true;
}

SaveAddressesRelations通常会保存人与地址之间的关系但是,几乎在顶部有一条线会阻止保存:

if (((force == false) 
            && (this.EntityState == CodeFluent.Runtime.CodeFluentEntityState.Unchanged)))
{
    return false;
}

如果此人的 CodeFluentEntityState 未更改。不会保存人员和地址之间的关系。

以下代码将保存关系:

var newAddress = new Address
{
    Street = "Saint Helena Road"
};
newAddress.Save();

var newPerson = new Person
{
    Name = "Napoleon"
};
newPerson.Addresses.Add(newAddress);
newPerson.Save();

在我看来,SaveAddressesRelations 应该放在 CodeFluentEntityState 检查之前,或者添加地址应该将 CodeFluentEntityState 切换为 Changed。

4

0 回答 0