首页 文章

属性'APPLICATIONID'是对象的关键信息的一部分,无法修改

提问于
浏览
0

我有一个问题,我无法通过寻找其他解决方案来解决它们 . 问题是我从用户那里得到了一个应用程序 . 应用程序可以处于不同的状态,我将应用程序本身保存在APPLICATION表中,并在APPLICATIONSTATE表中保存应用程序的状态 . 当用户提交应用程序时,应将默认应用程序状态值写入应用程序状态表 . 在此期间,应用程序的状态将被更改,每次更改都应写入APPLICATIONSTATE表中 .

我正在使用Entity Framework数据库第一种方法,(Oracle EF),这里是我的数据库E-R图:

database er diagram

以下是Entity Framework生成的部分类:

public partial class APPLICATION
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public APPLICATION()
    {
        this.APPLICATIONSTATE = new HashSet<APPLICATIONSTATE>();
    }

    public int APPLICATIONID { get; set; }      

    public System.DateTime APPLICATIONDATE { get; set; }

    public short PROVINCEID { get; set; }
    public Nullable<short> ILCEID { get; set; }
    public decimal AREA { get; set; }
    public bool ISBASEMENTOK { get; set; }
    public string APPLICATIONEXPLAINATION { get; set; }

    public virtual PERSONEL LOJ_PERSONEL { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<APPLICATIONSTATE> LOJ_APPLICATIONSTATE{ get; set; }

}

public partial class APPLICATIONSTATE
{
    public long APPLICATIONSTATEID { get; set; }
    public int APPLICATIONENUM { get; set; }
    public System.DateTime CURRENTTIME { get; set; }

    public virtual APPLICATION LOJ_APPLICATION { get; set; }
    public virtual USER LOJ_USER { get; set; }
}

我正在使用Repository模式,这是我尝试添加新应用程序和应用程序状态的代码部分:

int applicationId = int.minvalue;

    USER myuser = null;

    APPLICATION myApplication = this.gettingFromSomewhere();

    using (HousingEntities model = new HousingEntities())
    {
        applicantuser = model.LOJ_PERSONEL.FirstOrDefault(p => p.PERSONELID == YeniBasvuru.PersonelId);
        myApplication.LOJ_USER = applicantuser;
        model.APPLICATION.Add(myApplication);
        //saving the application
        model.SaveChanges();

        //getting the primary key of newly created application object from database.
        applicationId = this.getMostCurrentIdOfApplication();
        myApplication.APPLICATIONID = applicationId;

        //getting the user that submits application
        applicant = model.USER.FirstOrDefault(p => p.LOJ_USER.USERID == myApplication.LOJ_PERSONEL.USERID);

        //if clause-1
        if(applicant != null)
        {
            //saving the state of the application.
            appState = new APPLICATIONSTATE();

            //3 is the default state for application. When we need to change it to 4, newly row will be added. 
            appState.APPLICATIONSTATEID = 3;
            appState.LOJ_APPLICATION = myApplication;
            appState.LOJ_USER = applicant;

            //I am getting an error here. 
            model.APPLICATIONSTATE.Add(appState);
            model.SaveChanges();
        }
    }

    result.TransactionResult = true;
    result.rowId = applicationId;

}
//other catches removed for clarity.    
catch (Exception ex)
{
    islemSonuc = new FunctionResult ( ex, this._olasihataciddiyeti);
    this.writeError(ex);
}

我在上面指向的行中收到错误属性'APPLICATIONID'是对象的关键信息的一部分,无法修改 . 错误 . 我怎么能克服这个错误?提前致谢 .

我怎样才能克服这个错误?提前致谢 .

1 回答

  • 0

    Ivan Stoev先生的解决方案是正确的 . 在上面的代码中,

    appState.LOJ_APPLICATION = myApplication;

    是错误点 . 我们只通过设置实体的主键来进行分配,我们不去数据库 . 因此,EF不能仅与主键信息进行匹配,它还需要在后台添加附加信息 . 因此,如果我们更改代码如下:

    myApplication = model.APPLICATION(p => p.APPLICATIONID == applicationId)

    现在,myApplication包含数据库中最新鲜的信息 . 当我们分配它时,EF就能够匹配实体 .

相关问题