![](/img/trans.png)
[英]Error of Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query
[英]Dapper: Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query
我有這個用於將用戶插入數據庫的查詢:
INSERT INTO [User]
OUTPUT inserted.Id
VALUES (@IsDelete, @FirstName, @LastName, @UserName, @Email, @Birthdate, @IsActive, @Password, @SecurityStamp, @Gender, @ActivetionCode, @ActivationCodeExpireDateTime)
這是我的 model:
User user = new User();
user.Email = request.Email;
user.IsDelete = false;
user.IsActive = false;
user.Birthdate = DateTime.UtcNow;
user.FirstName = "user" + DateTime.Now.Millisecond;
user.LastName = "failmy" + DateTime.Now.Millisecond;
user.Password = request.Password;
user.UserName = request.UserName;
user.Gender = GenderEnum.Male;
user.SecurityStamp = Guid.NewGuid();
user.ActivetionCode = code;
user.ActivationCodeExpireDateTime = DateTime.UtcNow;
我在這里使用它:
var role = roleConnection.Query<int>(Command, Model);
但我收到此錯誤:
不允許從數據類型 datetime 到 int 的隱式轉換。 使用 CONVERT function 運行此查詢。
有什么問題? 我該如何解決?
正如所評論的:您的值列表和列列表之間可能存在不匹配,最終導致將值寫入它不屬於的列。
您正在使用未列出目標列的插入語法:
insert into [User] output inserted.Id values(val1, val2, ...)
這很容易出錯,並且當 go 出錯時很難調試。 相反,您應該枚舉插入查詢中的列:
insert into [User](col1, col2, ...) output inserted.Id values(val1, val2, ...))
使用此語法可以更輕松地檢查列和值是否匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.