Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

The property 'Key' is part of the object's key information and cannot be modified

$
0
0

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 classA. I have a concrete class B that contains A. Eventually I have a classC 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.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>