酒店“的applicationID”是对象的关键信息的一部分,不能被修改
问题描述:
我有一个问题,我是不是能够通过寻找其他的解决方案来解决这些问题。问题是我从用户那里得到一个应用程序。应用程序可以处于不同的状态,我将应用程序本身保存在APPLICATION表中,并将应用程序的状态保存在APPLICATIONSTATE表中。当用户提交应用程序时,默认的应用程序状态值应写入应用程序状态表。在此期间,应用程序的状态将被更改,并且每个更改都应写入APPLICATIONSTATE表中。酒店“的applicationID”是对象的关键信息的一部分,不能被修改
我使用实体框架数据库第一的方针,(甲骨文EF),这里是我的数据库ER图:
下面是由实体框架生成的部分类:
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”是对象的关键信息的一部分,不能进行修改。 error.How我可以克服这个错误?提前致谢。
我怎样才能克服这种错误?提前致谢。
答
伊万Stoev先生的解决方案是正确的。在上面的代码中,
appState.LOJ_APPLICATION = myApplication;
是错误点。我们只通过设置实体的主键来进行分配,我们不去数据库。因此,EF不能仅与主键信息进行匹配,它还需要在后台添加附加信息。所以,如果我们改变如下代码:
所有MyApplication = model.APPLICATION(P => p.APPLICATIONID ==的applicationID)
现在,所有MyApplication包含从数据库中最新鲜的资讯。当我们分配它时,EF能够匹配实体。
问题是'myApplication.APPLICATIONID = applicationId;'行。无论是'myApplication.APPLICATIONID'应该由数据库自动生成,你不需要该呼叫,或者你之前应该这样做**'model.APPLICATION.Add(所有MyApplication);' –
伊万,这不是重点。 applicationId是从数据库中获取的,当我删除if子句时,代码运行时没有错误。 – tahasozgen
哪条if子句? SaveChanges里面的那个? –