Ondřej Dobečka
Ondřej Dobečka

Reputation: 71

Exception when creating new entity in RIA

I use RIA + Silverlight (probably last versions), Entity Framework. When I insert a row with code

 var context = new DataService();
 var script = new Script { Id = Guid.NewGuid(), User = User.Id };
 context.Scripts.Add(script);
 context.SubmitChanges((o) =>
     {         
      if (!o.HasError)
       {} // OK, but has never gone here yet
      else
       {
         o.MarkErrorAsHandled();
         Messaging.MessageBox("some error" + o.Error.Message);
        }
      }, null);

... it throws exception


Cannot insert the value NULL into column 'Id', table Scripts, column does not allow nulls. INSERT fails.


When I try to debug, it goes well (to service), and that entity (Script) HAS SET 'Id' to some unique value. It looks like EntityFramework doesn't send that 'Id' to the database. It's possible ? :)

The 'Script' entity has only two columns: - Id (Guid), User (int), and hasn't any foreign key yet.

Did I miss to set something ? Thank you.

Upvotes: 2

Views: 300

Answers (2)

Ondřej Dobečka
Ondřej Dobečka

Reputation: 71

Ok, thank you for you answer. It probably could work if I updated my model from database. But I did it otherwise - created database from model.

Now I know, I have to set StoredGeneratedPattern from Identity to ** None**

Upvotes: 2

strattonn
strattonn

Reputation: 2002

Did you originally create the table without an Identity Specification column and then make the PK an identity? If so you need to Refresh your model.

Also make sure the [Key] attribute is on your PK.

Upvotes: 4

Related Questions