Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

entity framework error : {"The property 'Id' is part of the object's key information and cannot be modified. "}

During adding an instance of a class to the context following error is thrown how can i fix this pls? i use EF5 with MVC

{"The property 'Id' is part of the object's key information and cannot be modified. "}

below i also changed  [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] to  [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]  and it did not work, i also tried not passing Id at all or passing id during forming the instance in var comp = new Company { CompanyName = company.CompanyName}; but did not do any good unfortunately





mvc controller calls the repository function as below
 var comp = new Company { CompanyName = company.CompanyName};
            _repo.AddCompany(comp);

Open in new window


then error is thrown during :   _ctx.Companies.Add(newCompany); line below
 public bool AddCompany(Company newCompany)
        {
            try
            {
                _ctx.Companies.Add(newCompany);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

Open in new window



Company class is as follows;

 public class Company
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("Id")]
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public ICollection<Topic> Topics { get; set; }
    }

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

try changing
public int Id { get; set; }
to
public int Id { get;  }
Avatar of nicedone
nicedone

ASKER

@AndyAinscow,

after changing  to

public int Id { get; }

I get the error:

Company.Id get must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.

making it abstract eliminates the build error but then it makes the class abstract ,property abstract and in other parts of my application it caused other error which i did not fully check really.

do you think i should not implement auto property ?
And this (or similar - I'm trying to effective disable set) ?
public int Id { get; }
private Id { set; }
@AndyAinscow;

public int Id {get;private set;}  
did the magic. and it works now thanks

but did not understand fully why it was getting angry about? do you know why i ran into this issue?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might be getting error while updating EF, and Primary column must be changed!!  

Please check and verify the same
- Set a PK (Primary Key) on  ID column in Company table if not.
- Check XML code of myEntityModel and find: (Remove other columns and keep the primary/identity column only in <Key> section
<EntityType Name="Company">
    <Key>
        <PropertyRef Name="ID" />
        <PropertyRef Name="OtherColumnID" />   <!-- This second line caused problem -->
    </Key>
   ...
</EntityType>

Open in new window

- Remove Table<Company> from EntityModel and Add it again! Sometimes it works
- IF nothing works from above, remove .edmx and re-create it!
@Prakash Samariya

Please read the other comments, especially the one where it states it has been solved by doing what I suggested in the C# code behind.
@AndyAinscow;
"but did not understand fully why it was getting angry about? do you know why i ran into this issue?"
I think the author [nicedone] is curious to know the possible reasons, so I posted that one along-with good possible solutions!