【发布时间】:2017-01-09 11:38:10
【问题描述】:
我有一个问题,我无法通过寻找其他解决方案来解决它们。问题是我从用户那里得到了一个应用程序。应用程序可以处于不同的状态,我将应用程序本身保存在 APPLICATION 表中,并将应用程序的状态保存在 APPLICATIONSTATE 表中。当用户提交应用程序时,应将默认应用程序状态值写入应用程序状态表。随着时间的推移,应用程序的状态将发生变化,并且每次更改都应写入 APPLICATIONSTATE 表中。
我正在使用实体框架数据库第一种方法,(Oracle EF),这是我的数据库 E-R 图:
以下是实体框架生成的部分类:
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; }
}
我正在使用存储库模式,这是我尝试添加新应用程序和应用程序状态的代码部分:
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”是对象关键信息的一部分,无法修改。错误。我该如何克服该错误?提前致谢。
我怎样才能克服这个错误?提前致谢。
【问题讨论】:
-
问题是
myApplication.APPLICATIONID = applicationId;行。myApplication.APPLICATIONID应该由数据库自动生成并且您不需要该调用,或者您应该在 之前model.APPLICATION.Add(myApplication);执行此操作 -
伊万,这不是重点。 applicationId 是从数据库中获取的,当我删除 if 子句时,代码运行没有错误。
-
哪个 if 子句?里面有 SaveChanges 的那个?
-
问题标题中的
myID在哪里? -
好吧,当你删除
if子句时,你也删除了model.SaveChanges();哪个 IMO 正在生成异常,因为你已经更改了已经跟踪的myApplication对象的APPLICATIONID。跨度>
标签: c# entity-framework