我在我的MVC 5项目中使用Asp.Net Identity 2.0.
为什么列PhoneNumber使用[nvarchar](max)在SQL Server数据库中的表[dbo].[AspNetUsers]?
我可以将此更改为[nvarchar](64),例如?
服务定位器被认为是一种反模式。但是,如果它们仅在某些条件下使用,那么在构造函数中获取所有必要的依赖项是否正确?
方法一(服务定位器)
public class MyType
{
public void MyMethod()
{
if (someRareCondition1)
{
var dep1 = Locator.Resolve<IDep1>();
dep1.DoSomething();
}
if (someRareCondition2)
{
var dep2 = Locator.Resolve<IDep2>();
dep2.DoSomething();
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法二(构造函数注入)
public class MyType
{
private readonly IDep1 dep1;
private readonly IDep2 dep2;
public MyType(IDep1 dep1, IDep2 dep2)
{
this.dep1 = dep1;
this.dep2 = dep2;
}
public void MyMethod()
{
if (someRareCondition1)
{
dep1.DoSomething();
}
if (someRareCondition2)
{
dep2.DoSomething();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以有许多需要不同依赖项的不同空隙,但仅限于某些情况。使用服务定位器来提高性能和内存是否更好?
我使用 Entity Framework 6.2 并注意到 Distinct 忽略我的 OrderBy 子句。我有课:
public class Order
{
public int Id { get; set; }
public string PartnerId { get; set; }
// Other properties removed for clarity.
}
Run Code Online (Sandbox Code Playgroud)
并希望获得我订单的最后 20 个合作伙伴:
var list = db.Orders.AsNoTracking()
.OrderByDescending(obj => obj.Id)
.Select(x => x.PartnerId).Distinct().Take(20).ToList();
Run Code Online (Sandbox Code Playgroud)
生成的 SQL 查询忽略我的 OrderBy 子句:
SELECT
[Limit1].[PartnerId] AS [PartnerId]
FROM ( SELECT DISTINCT TOP (20)
[Extent1].[PartnerId] AS [PartnerId]
FROM [dbo].[Orders] AS [Extent1]
) AS [Limit1]
Run Code Online (Sandbox Code Playgroud)
如何从订单中准确获取最后 20 个合作伙伴?