Problem with SaveChange() in Entity Framework Data model - entity-framework-4

I am facing a problem with the SaveChanges() method in Entity Framework. Sometimes it works fine and sometimes it's not instead I get an error message saying:
String or binary data would be
truncated. The statement has been
terminated.
Can any one help me with this ....
thanks.

By default all strings stored as NVARCHAR(4000). In case while string length above 4000 chars you get this error. To limit field length and add validation logics to your model use StringLength(...maxlen...) attribute (http://msdn.microsoft.com/en-US/library/system.componentmodel.dataannotations.stringlengthattribute.aspx) with your model properties.

you can correct this problem on Data base Table on
"nvarchar" and "varchar Columns " that you increase Lenght of Column on your Table
also
when you insert or Update with EF , you can check len of value and check your query With
SQL Profiler when you call SaveChanges()method
and you check your value in GET and SET methods on Domain Layer or Entities that EF get you
and below link is Useful :
EF Exception: String or binary data would be truncated. The statement has been terminated.?
,
http://forums.asp.net/t/1660868.aspx/1

Related

How to find the distinct error messages of model state validation failure in asp.net mvc

I want to filter the Error Messages that gets populated as part of data annotation modelstate validation failure. As in if an array of objects comes as a part of class, and the validation fails for more than one object, I do not want the same message to be added again and again. Instead, I want to find the distinct error messages
string ValidationFailure= string.Join(";", actionContext.ModelState.Values.Distinct().Select(x.ErrorMessage));
But not able to get the required output.
It looks like your attempt is close, but you’re using Distinct on something that’s already unique (Values). Instead, try the following variation:
string ValidationFailure = string.Join(";", actionContext.ModelState.Values.Select(x => x.ErrorMessage).Distinct());
This ensures that you get a distinct list of ErrorMessages.

using locate function on calculated field in delphi

How can we use locate function or a same operation function using a calculated field in delphi Tadotable?
something like this
SampleAdotable.locate('samplefield',text,[lopartialkey]);
where samplefield is a calculated field in SampleAdotable.In normal case an exception with this message is created:
Item can not be found in the collection corresponding to the requested name or ordinal
thank you
If your SampleField is of type fkCalculated, I don't think you can use this field as a field whose value you try to locate in a call to Locate.
The reason is that Locate calls TCustomADODataSet.LocateRecord which generates the error you quote and the reason it does is that SampleField is not a field in the ADO Recordset underlying the TCustomADODataSet. The exception occurs in the call to Cursor.MoveNext.
To do what you want, try constructing a calculated field in the SQL expression used to obtain the row data from the database. Depending on the server you are using, you may need to use a TAdoQuery instead of a TAdoTable to get the rows.

Error selecting from large data table causing time out in EF + code first

I am using EF code first model to get the data from data base table in which i have 400,000 records.
But when i use the LINQ query something like:
var urer = context.UserEntity.Where(c => c.FirstName.Contains('s'));
The above statement gives me all the user to whose first name contains 's'. But since this is a huge data base table, it is giving me the following error:
An existing connection was forcibly closed by the remote host
Please suggest me the best way to do it. I am assigning this data to gridview. I am thinking to get the first 500 each time. Is there any way to do it from EF side, so that i won't need to do it in sql.
Thanks
1.add index on your column
2. increase timeout connection
You can create Store procedure
USE LINQ call Store procedure
LINQ to SQL (Part 6 - Retrieving Data Using Stored Procedures)
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
See this answer as well
Calling a SQL Server stored procedure with linq service through c#
Get rid of EF
set key in web.config common key for timeout replace 600
try
{
conn.Open();
mySqlCommand.Connection = conn;
mySqlCommand.CommandTimeout=600;

MVC EF String Length / Data Type for Signature Image

I'm using a javascript plugin called jSignature to give my MVC4 application delivery signature capture functionality. jSignature outputs the signature info in a data:image/png;base64,iVBORw0KG... string format that looks to be around 32,000 characters long. I created a property in my model called DeliverySignature as a string and am able to save and retrieve the signature data but when I pull it back in from the database it's only about 5,000 characters long. What data type do I need to be using in my model's definition and in the action method (it's being passed in to a controller to save to the db) so that it preservers the complete string length? Thank you.
I would store it in varchar(max) column.
You can keep model property type of string as strings do not have a limited length.

Why am I getting a "Unable to update the EntitySet because it has a DefiningQuery..." exception when trying to update a model in Entity Framework?

While updating with the help of LINQ to SQL using Entity Framework, an exception is thrown.
System.Data.UpdateException: Unable to update the EntitySet 't_emp' because it has
a DefiningQuery and no <UpdateFunction> element exists in the
<ModificationFunctionMapping>
The code for update is :
public void Updateall()
{
try
{
var tb = (from p in _te.t_emp
where p.id == "1"
select p).FirstOrDefault();
tb.ename = "jack";
_te.ApplyPropertyChanges(tb.EntityKey.EntitySetName, tb);
_te.SaveChanges(true);
}
catch(Exception e)
{
}
}
Why am I getting this error?
The problem was in the table structure. To avoid the error we have to make one primary key in the table. After that, update the edmx. The problem will be fixed
Three things:
Don't catch exceptions you can't handle. You're catching every exception possible, and then doing nothing with it (except swallowing it). That's a Bad Thing™ Do you really want to silently do nothing if anything goes wrong? That leads to corrupted state that's hard to debug. Not good.
Linq to SQL is an ORM, as is Entity Framework. You may be using LINQ to update the objects, but you're not using Linq to SQL, you're using Entity Framework (Linq to Entities).
Have you tried the solution outlined here? The exception you posted is somewhat cut off, so I can't be sure it's exactly the same (please update your post if it isn't), and if it is the same, can you comment on whether or not the following works for you?
"[..] Entity Framework doesn't know whether a given view is updatable
or not, so it adds the <DefiningQuery> element in order to safeguard
against having the framework attempt to generate queries against a
non-updatable view.
If your view is updatable you can simply remove the <DefiningQuery>
element from the EntitySet definition for your view inside of the
StorageModel section of your .edmx, and the normal update processing
will work as with any other table.
If your view is not updatable, you will have to provide the update
logic yourself through a "Modification Function Mapping". The
Modification Function Mapping calls a function defined in the
StorageModel section of your .edmx. That Function may contain the
name and arguments to a stored procedure in your database, or you can
use a "defining command" in order to write the insert, update, or
delete statement directly in the function definition within the
StorageModel section of your .edmx." (Emphasis mine, post formatted for clarity and for Stack Overflow)
(Source: "Mike" on MSDN)
But You can Set primary Key in Model if use MVC Asp.net
Just Open model.edmx in your table ,go to your field property and set Entity Key = True

Resources