Reputation: 443
I'm using Entity framework 6.1.3 on a .Net 4.5 WCF/HTTP app. I'm having some trouble saving new data.
my code looks like this :
using (AgpModel model = new AgpModel())
{
Entite.db.commentaire comment = commentaire.toDB();
model.commentaires.Add(comment);
model.SaveChanges();
commentaire.Id = comment.id;
}
AgpModel
is my context, commentaire
is my front model wich can be serialized over WCF, and comment
is the object i'm trying to save.
The toDB()
is the method transforming commentaire
into comment
.
When adding my very first data to the table, I'm having an exception on the call of SaveChanges()
which says :
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
I tried the method available at the link below only to have this exception :
OriginalValues cannot be used for entities in the Added state.
Reading on the table works fine.
Here is some extra code that can help you understand :
The toDB method :
public db.commentaire toDB()
{
return new db.commentaire() {
id = this.Id,
stagiaire = this.IdStagiaire,
utilisateur = this.IdUtilisateur,
niveau = (int)this.Niveau,
contenu = this.Contenu,
date = this.Date
};
}
The commentaire class :
[Table("agp.commentaires")]
public partial class commentaire
{
[Key]
[Column("commentaire", Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int utilisateur { get; set; }
[Key]
[Column("date", Order = 2, TypeName = "smalldatetime")]
public DateTime date { get; set; }
[Key]
[Column(Order = 3)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int stagiaire { get; set; }
[Key]
[Column(Order = 4)]
[StringLength(300)]
public string contenu { get; set; }
[Key]
[Column(Order = 5)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int niveau { get; set; }
public virtual utilisateur utilisateur1 { get; set; }
public virtual Stagiaire Stagiaire1 { get; set; }
}
Edit :
The request passed to sql server 2008 :
INSERT [agp].[commentaires]([utilisateur], [date], [stagiaire], [contenu], [niveau]) VALUES (@0, @1, @2, @3, @4)
SELECT [commentaire] FROM [agp].[commentaires] WHERE @@ROWCOUNT > 0 AND [commentaire] = scope_identity() AND [utilisateur] = @0 AND [date] = @1 AND [stagiaire] = @2 AND [contenu] = @3 AND [niveau] = @4
with those parameters :
Executed on my own this way and waorks ok :
DECLARE @0 as INT = 1;
DECLARE @1 AS DateTime2 = '30/07/2015 18:50:02';
DECLARE @2 AS int = 1786;
DECLARE @3 AS nvarchar(300) = 'coucou';
DECLARE @4 AS int = 2;
INSERT [agp].[commentaires]([utilisateur], [date], [stagiaire], [contenu], [niveau])
VALUES (@0, @1, @2, @3, @4)
SELECT [commentaire]
FROM [agp].[commentaires]
WHERE @@ROWCOUNT > 0 AND [commentaire] = scope_identity() AND [utilisateur] = @0 AND [date] = @1 AND [stagiaire] = @2 AND [contenu] = @3 AND [niveau] = @4
Still don't understand
Upvotes: 0
Views: 1162
Reputation: 2298
With your public DateTime date { get; set; }
set to Column(Typename="smalldatetime")
and your database appearing to expect a european date object. Perhaps passing a generic DateTime
to SQL is giving a smalldatetime
that is out of bounds for the expected minimum/maximum allowable DateTime
values.
I would suggest setting CultureInfo enGB = new CultureInfo("en-GB");
for your session as it appears your database may be specifying a specific date format. I would assume that Entity Framework would put the format in the format accepted in all cases:
YYYYMMDD
YYYY-MM-DDThh:nn:ss[.mmmmmmm]
However, your exception is not pointing to an exact column causing the issue. Your composite keys are all declared properly, and your toDB()
call appears to have populated all of the columns with data (though you'd need to set a breakpoint and validate that yourself). The only other thing I can think of that would trigger that error is an acceptable DateTime
being set in your model, but being converted into an SQL format that is out-of-bounds of the min/max range for smalldatetime
due to culture difference.
Upvotes: 1