public Salt(
decimal
id, decimal
fid, char[]
saltStr, IDataProvider
db ) {
Db = db; Salt
salt; if
( id != 0 ) {
salt = db.Salt.FirstOrDefault( i => i.FId == fid );
if ( salt !=
null
) {
Id = salt.Id;
FId = salt.FId;
SaltStr = saltStr;
} else {
Id = Db.Salt.Count() + 1;
FId = fid;
SaltStr = saltStr;
}
} else {
Id = id;
FId = fid;
SaltStr = saltStr;
}
Db.Salt.Add( this
); ( ( EFDbContext)
Db ).SaveChanges(); }
The issue is that an excption is being thrown when the SaveChanges() executes:
The class data members are:
[HiddenInput()] publicdecimal
Id { get;
set;
}
[HiddenInput()] publicdecimal
FId { get;
set;
}
publicchar[]
SaltStr { get;
set;
}
[NotMapped] publicIDataProvider
Db { get;
set;
}
The exception being thrown is:
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at My_MSI.Net.Models.Entities.Salt..ctor(Decimal id, Decimal fid, Char[] saltStr, IDataProvider db) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Entities\Salt.cs:line 55
at My_MSI.Net.Models.Entities.Identity.Verify(String clrPassword, Decimal memberId, IDataProvider db) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Entities\Identity.cs:line 107
at My_MSI.Net.Models.Concrete.FormsAuthProvider.Authenticate(String memberName, String password) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Concrete\FormsAuthProvider.cs:line 19
at My_MSI.Net.Controllers.AccountController.Login(Login details, String returnUrl) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Controllers\AccountController.cs:line 40
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`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Data.UpdateException
Message=An error occurred while updating the entries. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
Message=String or binary data would be truncated.
The statement has been terminated.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
class="16"
LineNumber=1
Number=8152
Procedure=""
Server=.\GEMSFAMILY
State=10
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
InnerException:
The only thing being inserted is a 32 character array into a Char(MAX) field in the database. The other two database fields are Decimal(18,0)
I changed the type to char in the Model class and that change the Model to store the 32 character array of chars under another name, and yes, the exact same error is still coming up when SaveChanges() executes.
eric2820
Contributor
2777 Points
1161 Posts
EF Issue
Mar 01, 2012 05:49 PM|LINK
I'm having an issue with EF in this class:
public Salt( decimal id, decimal fid, char[] saltStr, IDataProvider db )
{
Db = db;
Salt salt;
if ( id != 0 )
{
salt = db.Salt.FirstOrDefault( i => i.FId == fid );
if ( salt != null )
{
Id = salt.Id;
FId = salt.FId;
SaltStr = saltStr;
}
else
{
Id = Db.Salt.Count() + 1;
FId = fid;
SaltStr = saltStr;
}
}
else
{
Id = id;
FId = fid;
SaltStr = saltStr;
}
Db.Salt.Add( this );
( ( EFDbContext) Db ).SaveChanges();
}
The issue is that an excption is being thrown when the SaveChanges() executes:
The class data members are:
[HiddenInput()]
public decimal Id { get; set; }
[HiddenInput()]
public decimal FId { get; set; }
public char[] SaltStr { get; set; }
[NotMapped]
public IDataProvider Db { get; set; }
The exception being thrown is:
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
Message=An error occurred while updating the entries. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at My_MSI.Net.Models.Entities.Salt..ctor(Decimal id, Decimal fid, Char[] saltStr, IDataProvider db) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Entities\Salt.cs:line 55
at My_MSI.Net.Models.Entities.Identity.Verify(String clrPassword, Decimal memberId, IDataProvider db) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Entities\Identity.cs:line 107
at My_MSI.Net.Models.Concrete.FormsAuthProvider.Authenticate(String memberName, String password) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Models\Concrete\FormsAuthProvider.cs:line 19
at My_MSI.Net.Controllers.AccountController.Login(Login details, String returnUrl) in C:\Inetpub\wwwroot\My-MSI.Net\My-Msi.Net\My-MSI.Net\Controllers\AccountController.cs:line 40
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`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException: System.Data.UpdateException
Message=An error occurred while updating the entries. See the inner exception for details.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
InnerException: System.Data.SqlClient.SqlException
Message=String or binary data would be truncated.
The statement has been terminated.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
class="16"
LineNumber=1
Number=8152
Procedure=""
Server=.\GEMSFAMILY
State=10
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
InnerException:
The only thing being inserted is a 32 character array into a Char(MAX) field in the database. The other two database fields are Decimal(18,0)
So my question is: What is being truncated?!?
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: EF Issue
Mar 01, 2012 08:11 PM|LINK
Run sql server profiler. See the sql statement. Reproduce and modify values until no errror . Then you will find the bad value.
eric2820
Contributor
2777 Points
1161 Posts
Re: EF Issue
Mar 01, 2012 08:46 PM|LINK
Saying to use a tool (sql server profiler) without giving some instruction on how to access it is not very helpful.
We're only sending 32 chararacters of data and it's going into a table column defined as Char(MAX).
I was able to see the object model used within the EFDbClass and everything looks correct there:
Id = 1
FId = 1
SaltStr = 32 bytes of Char data
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
EnenDaveyBoy
Participant
1465 Points
1144 Posts
Re: EF Issue
Mar 01, 2012 09:31 PM|LINK
can you have a primary key as a decimal?
EnenDaveyBoy
Participant
1465 Points
1144 Posts
Re: EF Issue
Mar 01, 2012 09:37 PM|LINK
sorry just googled and yes they can
eric2820
Contributor
2777 Points
1161 Posts
Re: EF Issue
Mar 01, 2012 11:24 PM|LINK
I don't seem to have the sql server profiler installed on this HOME machine running Windows-7 64-Bit OS.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: EF Issue
Mar 02, 2012 04:36 AM|LINK
1. What sql server edition do you have?
2. If you modify the SaltStr value to be just 1 char, does it works?
eric2820
Contributor
2777 Points
1161 Posts
Re: EF Issue
Mar 02, 2012 09:31 AM|LINK
I'm using SQL Server 2008 R2 downloaded as a huge .exe file from MSDN.
It's still a Char[32] and I modified the table to hold a Char[32] as well, but still no joy.
The exception is still happening and the root cause still exists :-(
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: EF Issue
Mar 02, 2012 10:38 AM|LINK
did you test the application to post just one char? and does it shows again the error?
eric2820
Contributor
2777 Points
1161 Posts
Re: EF Issue
Mar 02, 2012 02:36 PM|LINK
I changed the type to char in the Model class and that change the Model to store the 32 character array of chars under another name, and yes, the exact same error is still coming up when SaveChanges() executes.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.