简体   繁体   English

实体框架6,无法添加新条目

[英]Entity Framework 6, cannot add a new entry

I'm using Entity framework 6.1.3 on a .Net 4.5 WCF/HTTP app. 我在.Net 4.5 WCF / HTTP应用程序上使用实体框架6.1.3。 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. AgpModel是我的上下文, commentaire是我可以在WCF上序列化的前端模型,并且comment是我要保存的对象。

The toDB() is the method transforming commentaire into comment . toDB()是将commentaire转换为comment的方法。

When adding my very first data to the table, I'm having an exception on the call of SaveChanges() which says : 当将我的第一个数据添加到表中时,在SaveChanges()调用中出现异常,该异常显示:

Store update, insert, or delete statement affected an unexpected number of rows (0). 存储更新,插入或删除语句影响了意外的行数(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. 有关了解和处理乐观并发异常的信息,请参见http://go.microsoft.com/fwlink/?LinkId=472540

I tried the method available at the link below only to have this exception : 我尝试了下面链接中可用的方法,但只有这个异常:

OriginalValues cannot be used for entities in the Added state. OriginalValues不能用于处于“已添加”状态的实体。

Reading on the table works fine. 在桌子上读书很好。

Here is some extra code that can help you understand : 这是一些额外的代码,可以帮助您理解:

The toDB method : toDB方法:

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 : 该请求传递给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 : 具有这些参数:

  • @0: '1' (Type = Int32) @ 0:“ 1”(类型= Int32)
  • @1: '30/07/2015 18:50:02' (Type = DateTime2) @ 1:“ 2015年7月30日18:50:02”(类型= DateTime2)
  • @2: '1786' (Type = Int32) @ 2:“ 1786”(类型= Int32)
  • @3: 'coucou' (Type = AnsiString, Size = 300) @ 3:“ coucou”(类型= AnsiString,大小= 300)
  • @4: '2' (Type = Int32) @ 4:“ 2”(类型= Int32)

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 还是不明白

With your public DateTime date { get; set; } 与您public DateTime date { get; set; } public DateTime date { get; set; } public DateTime date { get; set; } set to Column(Typename="smalldatetime") and your database appearing to expect a european date object. public DateTime date { get; set; }设置为Column(Typename="smalldatetime")并且您的数据库似乎期望使用欧洲日期对象。 Perhaps passing a generic DateTime to SQL is giving a smalldatetime that is out of bounds for the expected minimum/maximum allowable DateTime values. 也许将通用的DateTime传递给SQL可能会给出一个smalldatetime ,它超出了预期的最小/最大允许DateTime值的范围。

I would suggest setting CultureInfo enGB = new CultureInfo("en-GB"); 我建议设置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). 您所有的组合键都已正确声明,并且toDB()调用似乎已用数据填充了所有列(尽管您需要设置一个断点并验证自己)。 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. 我能想到的,会引发该错误的唯一的另一件事是可以接受的DateTime在你的模型设定,但被转换成SQL格式是出界外的最小/最大范围的smalldatetime由于文化的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM