SQL Server CE 4.0 - DbUpdate Exception was unhandled

喜欢而已 提交于 2019-12-13 20:53:24

问题


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 :

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.

来源:https://stackoverflow.com/questions/18175518/sql-server-ce-4-0-dbupdate-exception-was-unhandled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!