[英]Dapper.SimpleCRUD Insert issue without identity
我有表用户
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);
上面的代码抛出null exception.Table与身份工作正常。
堆栈跟踪:
在Dapper.SimpleCRUD.Insert [TKey](IDbConnection连接,Object entityToInsert,IDbTransaction事务,Nullable`1 commandTimeout)
错误信息:
Dapper.SimpleCRUD.dll中出现“System.Exception”类型的异常,但未在用户代码中处理其他信息:返回类型无效
我今天遇到了同样的问题。 这是因为您要指定要插入的类型,而不是指定将为int的返回类型。 尽管如此,你甚至不需要指定类型。
您的代码可以简化为:
var usr=new User();
usr.Id=1;
usr.Username="jhon";
usr.EmailID="j@gmail.com";
_dbConnection.Insert(usr);
以下是来自https://github.com/ericdc1/Dapper.SimpleCRUD的正确答案
[必需] - 默认情况下不插入[Key]属性,因为它应该是数据库自动增加的。 如果要在插入期间自己指定值,可以将属性标记为[Key]和[Required]。
就我而言,我的主键是long / bigint:
[Key]
[Required]
public long MyPrimaryKey { get; set; }
和T
在Insert<T>
需要匹配的返回类型(长):
connection.Insert<long>(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.