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

  • Question

  • 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.

    Thursday, March 21, 2019 8:25 AM

All replies

  • User753101303 posted

    Hi,

    The default value for an int is 0 and EF takes care of handling that properly as the value is assigned on the database side.

    Here it seems you want to use MinValue to tell about some error but turning an exception into a return value is considered bad : it is easy to forget about testing the returned value and actually trying to use it which is likely what happens here (basically EF you assigned a value to the key and complains about that).

    Not directly related but your intent is not clear. I assume you have another class that actually inherits from A ? Ah from the text is seems your intent is that C derives from A while it is actually B in your code...

    Thursday, March 21, 2019 9:03 AM