Note that there are some explanatory texts on larger screens.

plurals
  1. PODbDataConttoller UpdateEntity throws an exception
    primarykey
    data
    text
    <p>I'm experimenting with VS 2011 Beta and EF and I'm having trouble with UpdateEntity, but InsertEntity and DeleteEntity works fine.</p> <p>The problem is that when I call UpdateEntity I get an "object reference not set to an instace of an object" exception.</p> <p>Seems to happen with all the models in my project.</p> <p>To try to isolate the problem, I created a really simple DTO class with basic properties, but I get the same error when I try to call UpdateEntity on that object. Here's my code:</p> <p>The Test DTO:</p> <pre><code> [Table("Test")] public class Test { [Key, Column] public int Id { get; set; } [Column] public string TestMember { get; set; } } </code></pre> <p>I've got a DbDataController with the following methods:</p> <pre><code> public void InsertTest(Test entity) { InsertEntity(entity); } public void UpdateTest(Test entity) { UpdateEntity(entity); } public void DeleteTest(Test entity) { DeleteEntity(entity); } </code></pre> <p>Finally, I've defined this Test object in my DbContext class:</p> <pre><code>public DbSet&lt;Test&gt; Test { get; set; } </code></pre> <p>Of course I've got a table called Test in my database, this is the create script for the table:</p> <pre><code> CREATE TABLE [dbo].[Test]( [ID] [int] IDENTITY(1,1) NOT NULL, [TestMember] [nvarchar](50) COLLATE Icelandic_CI_AS NOT NULL, CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] </code></pre> <p>I can successfully insert new test objects to the database:</p> <pre><code> using (DataController dataSource = new DataController()) { Test test = new Test(); test.Id = 1; test.TestMember = "test2"; dataSource.InsertTest(test); dataSource.SaveChanges(); } </code></pre> <p>I can then easily delete it from the database:</p> <pre><code> using (DataController dataSource = new DataController()) { Test test = new Test(); test.Id = 1; test.TestMember = "test2"; dataSource.DeleteTest(test); dataSource.SaveChanges(); } </code></pre> <p>But if I try to do this:</p> <pre><code> using (DataController dataSource = new DataController()) { Test test = new Test(); test.Id = 1; test.TestMember = "UpdatedTest"; dataSource.UpdateTest(test); dataSource.SaveChanges(); } </code></pre> <p>I get a "Object reference not set to an instance of an object." exception at this line here:</p> <p><strong>UpdateEntity(entity);</strong></p> <p>inside the UpdateTest method in the DataController.</p> <p>There is no InnerException and the stack trace is not exactly helpful:</p> <blockquote> <p>at System.Web.Http.Data.EntityFramework.DbDataController<code>1.UpdateEntity(Object entity) at Hot.Web.Controllers.DataController.UpdateTest(Test entity) in c:\......\DataController.cs:line 48 at Hot.Web.Controllers.UserManagementController.EditProfile() in c:\.......\UserManagementController.cs:line 223 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary</code>2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c_<em>DisplayClass42.b</em>_41() at System.Web.Mvc.Async.AsyncResultWrapper.&lt;>c_<em>DisplayClass8<code>1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult</code>1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c</em>_DisplayClass37.&lt;>c_<em>DisplayClass39.b</em>_33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c_<em>DisplayClass4f.b</em>_49()</p> </blockquote> <p>Anyone got any clue what could be wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload