Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
EF & small date time
Message
Information générale
Forum:
ASP.NET
Catégorie:
MVC
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01570980
Message ID:
01571015
Vues:
46
Are you using CodeFirst? In that case just declare the date as SmallDateTime. In the entity designer there's a dropdown to pick type and the SqlDbTypes should all be there I believe (it's been a long time since I used the designer though so not sure what that looks like today).

+++ Rick ---

>Hi everybody,
>
>I am using EF and I have 2 small datetime columns in my table. I understand there is SqlDbType.SmallDateTime type but how should I declare these columns in my EF model?
>
>I am currently declaring them as DateTime and I am getting the error 'String or binary data will be truncated' executing this generated SQL:
>
>
>exec sp_executesql N'insert [dbo].[Clients]([client_no], [client_name], [Address], [Contact1], 
>[C1_Email], [C1_Phone], [C1_Ext], [Contact2], [C2_Email], [C2_Phone], [C2_Ext], 
>[EnteredBy], [ModifiedBy], [EnteredOn], [ModifiedOn])
>values (@0, @1, @2, @3, @4, @5, null, null, null, null, null, @6, null, @7, null)
>select [ClientId]
>from [dbo].[Clients]
>where @@ROWCOUNT > 0 and [ClientId] = scope_identity()',
>N'@0 smallint,@1 varchar(30),@2 varchar(max) ,@3 varchar(100),@4 varchar(100),@5 varchar(max) ,@6 char(6),@7 datetime2(7)',@0=789,@1='Naomi',@2='Test',@3='Naomi',@4='test@microsoft.com',@5='(817) 780-9054',@6='ADMIN ',@7='2013-04-15 13:47:46.3899786'
>
>where I suspect the error comes from the date column (EnteredOn) which I update manually this way:
>
>
> public void AddClient(Client client, bool autoCommit = true)
>        {
>            client.EnteredOn = DateTime.Now;
>            this.context.Clients.Add(client)   ;
>            context.Entry(client).Property(u => u.ModifiedBy).IsModified = false;
>            context.Entry(client).Property(u => u.ModifiedOn).IsModified = false;
>            client.EnteredBy = "ADMIN"; //constant for now
>            if (autoCommit) this.Commit();
>        }
>
>UPDATE. The error is actually for the Phone column. In my table I believe I assume the column is supposed to be saved without mask characters. I am wondering what is the best way to ensure that?
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform