错误:将实体添加到n:n关系不会更改CodeFluentEntityState

时间:2016-06-12 08:50:42

标签: codefluent

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

请修复它,这个bug会导致我的应用程序出现大问题..

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

<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>

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

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制作人生成的人员的保存方法如下所示:

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。

0 个答案:

没有答案