GET 调用:已添加具有相同键的项目。关键:身份证
GET Call: An item with the same key has already been added. Key: Id
我正在尝试使用 EF Core 调用 SQL 服务器数据库中的存储过程。数据库服务器使用 Docker 托管。问题是当我对控制器进行 GET 调用以访问服务时,它会停顿一会儿然后出现此错误...
ArgumentException: An item with the same key has already been added. Key: Id
这是我的代码:
型号
public interface IGiftList
{
int Id { get; set; }
string Occasion { get; set; }
int UserId { get; set; }
string Alias { get; set; }
bool AnotherRecipient { get; set; }
string RecipientName { get; set; }
int RecipientAddressId { get; set; }
}
public class GiftList : IGiftList
{
public int Id { get; set; }
public string Occasion { get; set; }
public int UserId { get; set; }
public string Alias { get; set; }
public bool AnotherRecipient { get; set; }
public string RecipientName { get; set; }
public int RecipientAddressId { get; set; }
public List<Gift> Items { get; set;}
}
控制器
[HttpGet, Route("lists")]
public List<GiftList> GetUserLists([FromQuery] int userId)
{
var lists = _giftService.GetUserLists(userId).Select(l => (GiftList)l).ToList();
return lists;
}
服务
public List<IGiftList> GetUserLists(int userId)
{
var items = this.giftContext.Lists.FromSqlRaw($"EXECUTE dbo.GetUserLists @UserId={userId}").ToList<IGiftList>(); <--Exception gets raised here.
return items;
}
存储过程
CREATE PROCEDURE dbo.GetUserLists
(
@UserId INT
)
AS
DECLARE @ListId INT
SET @ListId = (SELECT TOP (1) Id FROM Lists WHERE UserId = @UserId);
SELECT
L.Id,
O.Title,
U.Id,
L.Alias,
L.AnotherRecipient,
L.RecipientName,
L.RecipientAddressId
FROM Lists AS L
JOIN Occasions AS O
ON O.Id = L.OccasionId
JOIN Users AS U
ON U.Id = L.UserId
WHERE
L.UserId = @UserId
知道我为什么会收到此错误吗?
这是因为你的 Store Procedure's
两个字段 L.Id
和 U.Id
被视为 Id
但是当涉及到与你的模型绑定时 属性 是 Id
和 UserId
编译器在用两个映射时遇到问题。所以最好的方法是使用 SQL Alias AS
.
来处理这个问题
U.Id AS UserId
会解决您的问题。
Note: If the primary keys in the select query's are same remember to make them seperate by uisng AS
other than it will
mappped as same key.
我正在尝试使用 EF Core 调用 SQL 服务器数据库中的存储过程。数据库服务器使用 Docker 托管。问题是当我对控制器进行 GET 调用以访问服务时,它会停顿一会儿然后出现此错误...
ArgumentException: An item with the same key has already been added. Key: Id
这是我的代码:
型号
public interface IGiftList
{
int Id { get; set; }
string Occasion { get; set; }
int UserId { get; set; }
string Alias { get; set; }
bool AnotherRecipient { get; set; }
string RecipientName { get; set; }
int RecipientAddressId { get; set; }
}
public class GiftList : IGiftList
{
public int Id { get; set; }
public string Occasion { get; set; }
public int UserId { get; set; }
public string Alias { get; set; }
public bool AnotherRecipient { get; set; }
public string RecipientName { get; set; }
public int RecipientAddressId { get; set; }
public List<Gift> Items { get; set;}
}
控制器
[HttpGet, Route("lists")]
public List<GiftList> GetUserLists([FromQuery] int userId)
{
var lists = _giftService.GetUserLists(userId).Select(l => (GiftList)l).ToList();
return lists;
}
服务
public List<IGiftList> GetUserLists(int userId)
{
var items = this.giftContext.Lists.FromSqlRaw($"EXECUTE dbo.GetUserLists @UserId={userId}").ToList<IGiftList>(); <--Exception gets raised here.
return items;
}
存储过程
CREATE PROCEDURE dbo.GetUserLists
(
@UserId INT
)
AS
DECLARE @ListId INT
SET @ListId = (SELECT TOP (1) Id FROM Lists WHERE UserId = @UserId);
SELECT
L.Id,
O.Title,
U.Id,
L.Alias,
L.AnotherRecipient,
L.RecipientName,
L.RecipientAddressId
FROM Lists AS L
JOIN Occasions AS O
ON O.Id = L.OccasionId
JOIN Users AS U
ON U.Id = L.UserId
WHERE
L.UserId = @UserId
知道我为什么会收到此错误吗?
这是因为你的 Store Procedure's
两个字段 L.Id
和 U.Id
被视为 Id
但是当涉及到与你的模型绑定时 属性 是 Id
和 UserId
编译器在用两个映射时遇到问题。所以最好的方法是使用 SQL Alias AS
.
U.Id AS UserId
会解决您的问题。
Note: If the primary keys in the select query's are same remember to make them seperate by uisng
AS
other than it will mappped as same key.