已添加具有相同键的项目。关键:身份证
An item with the same key has already been added. Key: Id
我尝试使用以下 SQL 代码获取在数据透视 table 中没有关系的客户列表:
select * from [Identity].[Customer] as c
left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId
left outer join [Identity].[User] as u on u.id = uc.userId where
not exists (select userId,customerId
from [Identity].[UserCustomer] uc
where uc.userId = '0a65d716-7a15-4aa1-82f9-fce4418fbf1b'
and uc.customerId = c.id);
这在数据库中非常有效,我得到了我想要的结果。
但是当我尝试在我的控制器中实现它并将它传递给我的视图时,我在访问该页面时收到以下错误:
这是我的代码:
索引控制器
public ActionResult Index()
{
var user = _usermanager.GetUserId(HttpContext.User);
var customer = _db.Customer.FromSqlRaw("select * from [Identity].[Customer] as c left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId left outer join [Identity].[User] as u on u.id = uc.userId where not exists (select userId,customerId from [Identity].[UserCustomer] uc where uc.userId = {0} AND uc.customerId = c.id)",user).ToList();
return View(customer);
}
查看
@model List<customer>
<h1>list with customers</h1>
@*create a new customer*@
<a asp-controller ="Customer" asp-action="Create">create new customer</a>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">firstname</th>
<th scope="col">lastname</th>
<th scope="col">adress</th>
<th scope="col">phonenumber</th>
<th scope="col">edit</th>
<th scope="col">claim this user</th>
</tr>
</thead>
<tbody>
@foreach(var customers in @Model)
{
<tr>
<td>@customers.Id</td>
<td>@customers.firstname</td>
<td>@customers.lastname</td>
<td>@customers.adress</td>
<td>@customers.phonenumber</td>
<td><a asp-controller= "customer" asp-action="edit" asp-route-id = "@customers.Id">edit</a></td>
<td><a asp-controller= "customer" asp-action="claimCustomer" asp-route-id = "@customers.Id">claim</a></td>
</tr>
}
</tbody>
</table>
出现这个问题是因为在不同的 table 中有同名的列。
您应该指定 table 的列 returned 而不是使用通配符 *
到 return 所有列。
string query = @"
select c.Id, c.FirstName, c.LastName --And other columns to be returned
from [Identity].[Customer] as c
left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId
left outer join [Identity].[User] as u on u.id = uc.userId where
not exists (select userId,customerId
from [Identity].[UserCustomer] uc
where uc.userId = {0}
and uc.customerId = c.id);
";
var customer = _db.Customer.FromSqlRaw(query, user).ToList();
我尝试使用以下 SQL 代码获取在数据透视 table 中没有关系的客户列表:
select * from [Identity].[Customer] as c
left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId
left outer join [Identity].[User] as u on u.id = uc.userId where
not exists (select userId,customerId
from [Identity].[UserCustomer] uc
where uc.userId = '0a65d716-7a15-4aa1-82f9-fce4418fbf1b'
and uc.customerId = c.id);
这在数据库中非常有效,我得到了我想要的结果。
但是当我尝试在我的控制器中实现它并将它传递给我的视图时,我在访问该页面时收到以下错误:
这是我的代码:
索引控制器
public ActionResult Index()
{
var user = _usermanager.GetUserId(HttpContext.User);
var customer = _db.Customer.FromSqlRaw("select * from [Identity].[Customer] as c left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId left outer join [Identity].[User] as u on u.id = uc.userId where not exists (select userId,customerId from [Identity].[UserCustomer] uc where uc.userId = {0} AND uc.customerId = c.id)",user).ToList();
return View(customer);
}
查看
@model List<customer>
<h1>list with customers</h1>
@*create a new customer*@
<a asp-controller ="Customer" asp-action="Create">create new customer</a>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">firstname</th>
<th scope="col">lastname</th>
<th scope="col">adress</th>
<th scope="col">phonenumber</th>
<th scope="col">edit</th>
<th scope="col">claim this user</th>
</tr>
</thead>
<tbody>
@foreach(var customers in @Model)
{
<tr>
<td>@customers.Id</td>
<td>@customers.firstname</td>
<td>@customers.lastname</td>
<td>@customers.adress</td>
<td>@customers.phonenumber</td>
<td><a asp-controller= "customer" asp-action="edit" asp-route-id = "@customers.Id">edit</a></td>
<td><a asp-controller= "customer" asp-action="claimCustomer" asp-route-id = "@customers.Id">claim</a></td>
</tr>
}
</tbody>
</table>
出现这个问题是因为在不同的 table 中有同名的列。
您应该指定 table 的列 returned 而不是使用通配符 *
到 return 所有列。
string query = @"
select c.Id, c.FirstName, c.LastName --And other columns to be returned
from [Identity].[Customer] as c
left outer join [Identity].[UserCustomer] as uc on c.id = uc.customerId
left outer join [Identity].[User] as u on u.id = uc.userId where
not exists (select userId,customerId
from [Identity].[UserCustomer] uc
where uc.userId = {0}
and uc.customerId = c.id);
";
var customer = _db.Customer.FromSqlRaw(query, user).ToList();