User-234026806 posted
Hi There;
I am writing a software and I have a problem that I cannot figure out.
I have used inheritance in my project and Table per Hierarchy. I have an abstract class
A. I have a concrete class B that contains A. Eventually I have a class
C that derives from class A. "Anahtar" is the Turkish definition of key, I have used the word "Key" in the headline for comprehensibleness.
Here is my class structures:
public abstract class A
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Anahtar { get; set; }
[Required]
[StringLength(350)]
public string Adi { get; set; }
//constructors removed for brevity
}
public class B
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Anahtar { get; set; }
[Required]
public A ObjectOfA { get; set; }
}
public class C: B
{
[StringLength(1000)]
public string YanEtkisi { get; set; }
}
Here is my repository addition operation:
public int AddC(C cValue)
{
try
{
A objectOfA = null;
int sonucId = int.MinValue;
if (!this._kontrol.uygunMuCrudIslemine(cValue))
throw new ArgumentNullException();
objectOfA = this._context.ObjectOfALists.FirstOrDefault(p=>p.Anahtar == cValue.ObjectOfA.Anahtar);
if (objectOfA == null)
throw new ApplicationException();
cValue.ObjectOfA = objectOfA;
this._context.ObjectOfCLists.Add(cValue);
this._context.SaveChanges();
this._context.Entry(cValue).GetDatabaseValues();
sonucId = cValue.Anahtar;
return sonucId;
}
catch (Exception hata)
{
this._hata.YazHata(hata);
}
return int.MinValue;
}
When I run the code in different scenarios, I am getting error of InvalidOperationException .
Here is the error definition:
mes: The property 'Anahtar' is part of the object's key information and cannot be modified.
There is the errorenous sample object's variables:
<B's Anahtar: -2147483648> (int.Minvalue, default value for int)
<Yan etkisi : 1786460434>
A's Anahtar: 3>
How can I solve that problem? Thanks in advance.