3

我有表用户

CREATE TABLE [User](
    [Id] [int] primary key NOT NULL,
    [Username] [nvarchar](50) NULL,
    [EmailID] [nvarchar](50) NULL)

用户类

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string EmailID { get; set; }
}

代码

var usr=new User();
usr.Id=1;
usr.Username="jhon";
usr.EmailID="j@gmail.com";
_dbConnection.Insert<User>(usr);

上面的代码抛出空异常。具有身份的表工作正常。

堆栈跟踪:

在 Dapper.SimpleCRUD.Insert[TKey](IDbConnection 连接,对象 entityToInsert,IDbTransaction 事务,Nullable`1 commandTimeout)

错误信息:

Dapper.SimpleCRUD.dll 中出现“System.Exception”类型的异常,但未在用户代码中处理附加信息:无效的返回类型

4

2 回答 2

7

我今天遇到了同样的问题。这是因为您指定了要插入的类型,而不是指定返回类型为 int。话虽如此,您甚至不需要指定类型。

您的代码可以简化为:

var usr=new User();
usr.Id=1;
usr.Username="jhon";
usr.EmailID="j@gmail.com";
_dbConnection.Insert(usr);
于 2016-04-06T19:32:24.660 回答
4

这是来自https://github.com/ericdc1/Dapper.SimpleCRUD的正确答案

[必需] - 默认情况下,不插入 [Key] 属性,因为它预计会由数据库自动递增。如果您想在插入过程中自己指定值,可以将属性标记为 [Key] 和 [Required]。

就我而言,我的主键是 long / bigint:

    [Key]
    [Required]
    public long MyPrimaryKey { get; set; }

并且需要匹配返回类型(long)TInsert<T>

connection.Insert<long>(model);
于 2018-04-30T17:57:13.660 回答