我目前正在编写一个简单的 Web 应用程序,它利用 mssql 数据库来存储数据。就像现在一样,我尝试设置我的业务逻辑层,从而以最好的方式构建它。
目前,我所有的调用都是使用 Linq 进行的,实际上它们都不是异步的。我对异步编程还很陌生,并且通常很想知道我应该对哪些查询使用异步调用。
据我所知,使用异步数据库调用是有意义的,因为它可以释放主线程上的内存并且不会阻塞 UI。此外,异步调用可以并发运行,这意味着可以同时完成更多事务。
但是,考虑到我在我的上下文中异步添加多个实体,并使用该SaveChangesAsync()方法来结束我的上下文。AddAsync()在我的上下文中拥有多个方法不是多余的,因为SaveChangesAsync()无论如何都会异步运行所有内容吗?
最后,根据我所阅读和所见,很多人更喜欢使用异步数据库调用。也就是说,与其说是例外,不如说是规则。
如果是这样,在什么情况下我会避免异步数据库调用,在我的业务逻辑层中添加异步调用时我应该遵守哪些一般规则?
c# asynchronous entity-framework linq-to-sql task-parallel-library
我刚刚安装了 Visual Studio 2019,我正在尝试添加一个带有 ADO.NET 实体模型的数据库,但是向导在这一步崩溃了......
我有 Visual Studio Community 2019 v16.4.5
Installed MySQL for Visual Studio 1.2.9
Installed MySQL connector net 8.0.19
我添加了对我的项目
MySQL.data
MySQL.data.EntityFramework for EF6 MySQL.web 的
引用
所有版本 8.0.19,与连接器相同
我还尝试从 nuget 包中添加
EntityFramework
MySqlData
MySql.Data.Entity
MySql.Web
并将其添加到 web.config
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
Run Code Online (Sandbox Code Playgroud)
并尝试了我读过的所有内容,但它仍然不起作用,之后它只是崩溃,或者它给了我这个错误
有人可以帮忙吗?我要添加旧版本吗?
mysql entity-framework mysql-connector visual-studio visual-studio-2019
我想将 ComboBox 绑定到 53k 行的 EF Core 实体。这需要一些时间,大约 10 秒。
我认为如果我将绑定过程放在 FormShown事件中,UI 将保持响应。但事实并非如此。
我试过的:
Private Sub frmCerere_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Task.Factory.StartNew(Sub() GetProducts(cmbProduse), TaskCreationOptions.LongRunning)
End Sub
Public Shared Sub GetProducts(ctrl As ComboBox)
Using context As EnsightContext = New EnsightContext
context.Produse.Load()
Dim idsap = context.Produse.Local.Select(Function(o) o.IdSap).ToList
ctrl.DataSource = idsap
End Using
End Sub
Run Code Online (Sandbox Code Playgroud)
无济于事,因为什么都没有发生。显示了 Form,但 ComboBox 是空的。
如何将 ComboBox 返回到主线程?
从 SQL 表中,我试图获取每个项目的最后一行。我分配了一个用户列表(objectIds 列表)并希望获得他们每个人的最后一份工作。这是下面的功能。
public async Task<List<Job>> GetLastJobs(List<int> objectIds)
{
using ManagerContext context = new ManagerContext(_callContext);
List<Job> jobs = context.Jobs.Where(j => j.ObjectId.HasValue && objectIds.Contains(j.ObjectId.Value)).GroupBy(j => j.ObjectId).Select(j => j.OrderByDescending(p => p.Id).FirstOrDefault()).ToList();
return null;
}
Run Code Online (Sandbox Code Playgroud)
在执行时,它返回:
the LINQ expression '(GroupByShaperExpression:
KeySelector: (j.ObjectId),
ElementSelector:(EntityShaperExpression:
EntityType: Job
ValueBufferExpression:
(ProjectionBindingExpression: EmptyProjectionMember)
IsNullable: False
)
)
.OrderByDescending(p => p.Id)' could not be translated.
Run Code Online (Sandbox Code Playgroud)
以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。有关 详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。
我不知道如何,从哪里开始解决问题
SQL ServerFileTable于 2012 年首次推出,但它不受实体框架(.NET Core 或完整的 .NET Framework)支持。使用 SQL ServerFileTable或FileStream允许更快的文件上传和下载。
我想FileTable与我的 .NET Core 应用程序一起使用,我必须在其中创建一个FileTable与另一个简单表的关系。
为此,我需要hierarchyid在 C# 中输入类型并在 SQL Server 实例级别启用文件流支持。Entity FrameworkFileTable在更新迁移期间创建数据库时,似乎没有提供对创建数据库的支持。
所以这是我的第一个 Stack Overflow 问题,我希望我能提供足够的帮助细节。我正在尝试将 AutoMappper 与 dotnet 3.1 与依赖注入一起使用,但是它似乎并没有像它所说的那样注册我的地图。
我收到的错误:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
User -> UserCustomerDto
Run Code Online (Sandbox Code Playgroud)
在我的 Startup.cs 中,我有:services.AddAutoMapper(typeof(Startup));在该区IServiceCollection域内
我User创建了我的模型和各种 DTO 模型。
这是我拥有的示例 Profile 模型:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
User -> UserCustomerDto
Run Code Online (Sandbox Code Playgroud)
我的 DTO 模型具有我的用户模型所具有的所有字段,但我不想显示在 UI 中的那些字段。
文档相当难以遵循,因为有非依赖注入的示例以及在项目启动期间需要配置的 9.0 之前的示例。
我也尝试.ReverseMap()在 上使用,CreateMap()但这也不起作用。
我正在使用的服务中注入:
public class UserCustomerProfile : Profile
{
public UserCustomerProfile()
{
CreateMap<User, UserCustomerDto>();
CreateMap<UserCustomerDto, User>();
} …Run Code Online (Sandbox Code Playgroud) c# entity-framework dependency-injection automapper asp.net-core
假设我在 EF Core 中有一个查询,如下所示:
MethodWithDomainLogic() {
return this.dbContext.People.Where(...).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
现在,为了将我的域层与我的数据访问层分离,我希望这个方法在我的域程序集中,并且该程序集没有对 EFCore 的引用。所以它看起来像:
在领域层:
public interface IEntities {
IQueryable<Person> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在数据层:
private IEntities entities; // injected with a dbcontext that implements the IEntities interface.
public Task<IEnumerable<Person>> MethodWithDomainLogic() {
return this.entities.People.Where(...).ToListAsync(); // How to do this without referencing EFCore?
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这是不可能的,因为 Linq 本身不支持异步查询。
我已经通过在域层中使用 EFCore 接口设置我自己的接口解决了这个问题(基本上是复制并粘贴了 ToListAsync 和 FirstAsync 等方法......)然后在我的数据层中实现它,只需调用EFCore 版本。然而,这是非常混乱和复杂的。
有更好的解决方案吗?我希望我的域方法在没有 EFCore 的情况下使用异步编程和 Linq,但要在数据层中使用 EFCore 实现 Linq/异步编程。
我正在试验 .net 核心、实体框架和 sqlserver,我认为hierarchyID 是我想要建模的完美数据类型。但是当我搭建数据库时,EFC 无法映射层次结构。
Could not find type mapping for column 'dbo.ingredient.hierarchy_id' with data type 'hierarchyid'. Skipping column.
Could not scaffold the primary key for 'dbo.ingredient'. The following columns in the primary key could not be scaffolded: hierarchy_id.
Unable to generate entity type for table 'dbo.ingredient'.
Could not find type mapping for column 'dbo.ingredient_location.ingredient_id' with data type 'hierarchyid'. Skipping column.
Could not find type mapping for column 'dbo.locations.location' with data type 'geography'. Skipping column.
Could not find type …Run Code Online (Sandbox Code Playgroud) 我正在尝试将字符串参数传递给 SQL 查询,在下面接收错误。我该如何解决这个问题?当前使用答案EF Core 2.2,将字符串参数传递给 FromSql 语句
输入字符串的格式不正确。
public async Task<IEnumerable<Product>> GetProduct(string productKey)
{
var productParameter = new SqlParameter("@productKey", SqlDbType.VarChar);
productParameter.Value = productKey;
var productIdList = db.TestDb
.FromSql($"select ProductId from dbo.Product product" +
" (where product.ProductKey = {productParameter})" )
.Select(c => c.ProductId).ToList();
Run Code Online (Sandbox Code Playgroud)
它是来自 ProductKey 的类型 varchar(6)
使用网络核心 2.2
我正在编写FromSql使用 InMemory 数据库测试 语句。我们正在尝试使用 Sqlite。
运行下面的Sql通过单元测试没有报错。
select * from dbo.Product
Run Code Online (Sandbox Code Playgroud)
但是,这样做也会通过不正确的 sql 语法。想要使用不正确的 sql 语法使测试失败。我们如何正确测试 FromSql?
没有错误来自错误的语法。
seledg24g5ct * frofhm dbo.Product
Run Code Online (Sandbox Code Playgroud)
完整代码:
namespace Tests.Services
{
public class ProductTest
{
private const string InMemoryConnectionString = "DataSource=:memory:";
private SqliteConnection _connection;
protected TestContext testContext;
public ProductServiceTest()
{
_connection = new SqliteConnection(InMemoryConnectionString);
_connection.Open();
var options = new DbContextOptionsBuilder<TestContext>()
.UseSqlite(_connection)
.Options;
testContext= new TestContext(options);
testContext.Database.EnsureCreated();
}
[Fact]
public async Task GetProductByIdShouldReturnResult()
{
var productList = testContext.Product
.FromSql($"seledg24g5ct * frofhm dbo.Product");
Assert.Equal(1, 1);
} …Run Code Online (Sandbox Code Playgroud) entity-framework ×10
c# ×6
.net ×3
linq ×2
sql-server ×2
.net-core ×1
asp.net-core ×1
asynchronous ×1
automapper ×1
decoupling ×1
hierarchyid ×1
linq-to-sql ×1
mysql ×1
scaffolding ×1
sqlite ×1
task ×1
vb.net ×1
winforms ×1