0

I am struggling with SQL Server CE 4.0 almost all day. I get this error when I try to save new record (entity) to my database.

First a little preview - I installed SQL Server CE 4.0 via NuGet and I created a database for my project. Then I created the Entity Framework model layer and started working with that.

When I get this error my inner exception says this :

The column cannot be modified. [ Column name = Id ]

I did a little research and find out that this may be cause because of my settings for the Id property so I changed it like that :

Id Settings

I already have 17 records that I have inserted manually for testing purposes but I doubt this may cause any problem.

So what I try to do is this:

public override void Save()
{
            using (RalBaseEntities ctx = new RalBaseEntities())
            {
                System.Data.Entity.DbSet<MainInfo> mainInfoEntity = ctx.Set<MainInfo>();

                MainInfo entity = new MainInfo();
                entity.Manager = txtManager.Text;
                entity.Broker = txtBroker.Text;

                mainInfoEntity.Add(entity);
                ctx.SaveChanges();               
            }
}

So when I try to execute the Save() method I get the error above. I wrote that in previous versions one should have to create the id manually, but it's fixed in v. 4.0 and judging by the settings that I show as an image here I don't see a reason just to get a new record with an auto generated unique Id.

The word Update in DbUpdate Exception that I get is worrying me a bit. Maybe I'm trying to save the data in a wrong way but I spend a lot of time googling and it seems to be the right way.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Leron
  • 9,546
  • 35
  • 156
  • 257
  • You need to specify that the Id value is store generated in your model – ErikEJ Aug 11 '13 at 19:26
  • Could you be more specific, I'm not sure what do you mean exactly. Maybe I need to provide some additional code? – Leron Aug 11 '13 at 19:31
  • Do you use edmx or code first? If code first, add the Key attribute to the primary key field – ErikEJ Aug 12 '13 at 05:39
  • I use edmx. I've been working with code first and was wondering if I should/could add this manually in my edmx but decided that it's not meant to be made that way. – Leron Aug 12 '13 at 16:29

0 Answers0