小编Gle*_*leb的帖子

在 EF Core 3.1 中包含 FromSqlRaw 和存储过程

所以这是交易 - 我目前正在使用 EF Core 3.1,假设我有一个实体:

public class Entity
{
    public int Id { get; set; }
    public int AnotherEntityId { get; set; }
    public virtual AnotherEntity AnotherEntity { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我以DbSet<Entity> Entities正常方式访问时,我包括另一个实体,如:

_context.Entities.Include(e => e.AnotherEntity)

这有效。为什么不会,对吧?然后我去:

_context.Entities.FromSqlRaw("SELECT * FROM Entities").Include(e => e.AnotherEntity)

这也有效。两者都返回与另一个实体连接的相同对象集合。然后我使用一个存储过程,它由SELECT * FROM Entities名为 spGetEntities的相同查询组成:

_context.Entities.FromSqlRaw("spGetEntities")

你猜怎么着?这也有效。它给了我相同的输出,但显然没有加入另一个实体。但是,如果我尝试像这样添加 Include:

_context.Entities.FromSqlRaw("spGetEntities").Include(e => e.AnotherEntity)

我正进入(状态:

FromSqlRaw 或 FromSqlInterpolated 是用不可组合的 SQL 调用的,并且查询在它上面组合。考虑AsEnumerable 在 FromSqlRaw 或 FromSqlInterpolated 方法之后调用以在客户端执行组合。

即使_context.Entities.FromSqlRaw("SELECT * FROM Entities")和的输出_context.Entities.FromSqlRaw("spGetEntities") …

.net c# entity-framework join entity-framework-core

17
推荐指数
1
解决办法
3万
查看次数

如何在c#泛型中将子类转换为父类

我有一个课程:

class AClass : CClass
class BClass : CClass
Run Code Online (Sandbox Code Playgroud)

我需要在以下代码中使用它们:

POJO 类:

class SomeObject<T> where T : CClass
    T clazz;
    setClass(T clazz){
        this.clazz = clazz;
    .....
Run Code Online (Sandbox Code Playgroud)

控制器类:

class ObjectExecutor{   

    private SomeObject<CClass> someObject;

    public ObjectExecutor execute(SomeObject<CClass> so){
        someObject = so; //exception when so - is the instance of AClass 
        return this;
    }

    public void build(){
        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

所以当我尝试实现它时:

SomeObject<AClass> soA = new....
soA.execute(soA) //exception point
    .build()
Run Code Online (Sandbox Code Playgroud)

结果我得到了异常: can't cast AClass to CClass

所以我需要使用类似的东西:

class ObjectExecutor{   

    private SomeObject someObject; //without <> …
Run Code Online (Sandbox Code Playgroud)

c# generics covariance contravariance

3
推荐指数
1
解决办法
137
查看次数

有没有一种方法可以使正在运行的方法立即停止使用 cts.Cancel();

我有创建 CancellationTokenSource 并将其传递给方法的代码。

我在另一个发出 cts.Cancel(); 的应用程序中有代码。

有没有一种方法可以使该方法立即停止,而不必等待 while 循环内的两行完成?

请注意,如果它导致我可以处理的异常,我会没事的。

public async Task OnAppearing()
{
   cts = new CancellationTokenSource();
   await GetCards(cts.Token);
}

public async Task GetCards(CancellationToken ct)
{
   while (!ct.IsCancellationRequested)
   {
      App.viewablePhrases = App.DB.GetViewablePhrases(Settings.Mode, Settings.Pts);
      await CheckAvailability();
   }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading task cancellationtokensource

1
推荐指数
1
解决办法
229
查看次数