我正在尝试使用C#在VS 2015 Pro(Update 3)中创建Web API并以.NET Core为目标.
我正在学习本教程:https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework.但是,我连接的是MySQL数据库而不是SQL Server数据库 - 不确定它有多大区别......
无论如何,在教程中,我必须"使用依赖注入注册我的上下文" - 所以我必须将以下行添加到Startup.cs文件的ConfigureServices部分:
var connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;
Run Code Online (Sandbox Code Playgroud)
但是,VS给了我以下错误:
错误CS1061'DbContextOptionsBuilder'不包含'UseSqlServer'的定义,并且没有可以找到接受类型'DbContextOptionsBuilder'的第一个参数的扩展方法'UseSqlServer'(您是否缺少using指令或程序集引用?)
有什么想法吗?
这就是整个Startup.cs文件的样子:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace PropWorxAPI
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot …
Run Code Online (Sandbox Code Playgroud) mysql asp.net-mvc entity-framework asp.net-web-api asp.net-core
我有一个客户端程序(.Net 4.8 上的 WPF 应用程序)和一个 Web API(.Net Core 3.1)。我正在尝试让两者通过 SignalR Core 进行通信。当两者都在我的电脑上本地运行时(即在本地主机上),它可以完美地工作。但是,一旦我将 API 发布到 Azure 应用服务(并将 WPF 应用程序指向新 URL),它就不起作用了。SignalR 建立连接,但当 API 向 WPF 应用程序发送数据时,应用程序永远不会收到它。
不知道和CORS有没有关系。Azure 应用服务上的 CORS 已禁用。在我的 Web API 上,我使用以下 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
});
string connectionString = Configuration.GetConnectionString("eBallDatabase");
services.AddDbContext<EBallContext>(options =>
options.UseSqlServer(connectionString));
var config = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
});
services.AddApplicationInsightsTelemetry(); …
Run Code Online (Sandbox Code Playgroud) 在 .Net 5 Web API 中,我想运行一个发送批量电子邮件和 SMS 的后台任务。我知道我可以创建一个从 BackgroundService 继承的服务,然后将它添加到 Startup.ConfigureServices 方法中的 DI 容器,如下所示:
services.AddHostedService<EmailAndSmsService>();
Run Code Online (Sandbox Code Playgroud)
但这会立即运行服务 - 即在应用程序启动时。我想在 API 收到来自前端的请求时运行该服务。即在控制器的动作方法中。
我一直在查看 Microsoft 文档中的“带有托管服务的后台任务”,如果我没记错的话,这就是我需要做的(查看标题为“在后台任务中使用范围服务”的部分):
这样对吗?我是否基本上需要创建两个服务,一个执行实际工作,另一个调用执行实际工作的服务?我在正确的道路上吗?
谢谢
c# background-service .net-core asp.net-core asp.net-core-hosted-services
(使用Entity Framework 6.2)
我有以下两个模型/实体:
public class City
{
public int CityId { get; set; }
public string Name { get; set; }
}
public class Country
{
public Country()
{
Cities new HashSet<City>();
}
public int CountryId { get; set; }
public string Name { get; set; }
public virtual ICollection<City> Cities { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及下面的DbContext
public DbSet<Country> Countries { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的问题是:如果Country对象的子项发生了变化(即城市),我该如何更新?
我可以这样做:
List<City> cities = new List<City>();
// Add a couple of cities to the …
Run Code Online (Sandbox Code Playgroud) 我有一个ASP .Net Core 1.1 Web Api,它可以处理来自前端Web应用程序和电话应用程序的请求。该应用程序供房地产经纪人执行对财产的检查。因此,系统具有诸如检查,财产,用户,代理等的实体。用户(即房地产经纪人)和财产属于代理。因此,房地产经纪人可以拥有一个或多个用户以及一个或多个财产。
例如,用户可能想查看由其房地产经纪人中的任何人检查的所有财产的列表。在这种情况下,他单击Web /电话应用程序中的某个位置,然后该应用程序向HTTP发送HTTP GET请求以检索属性列表。Web API在接收到此请求后,首先检查哪个用户正在请求此请求,然后检查该用户所属的代理,然后返回属于该代理的所有属性。所以...这些是我拥有的[简化]模型:
[Table("agency")]
public class Agency
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("agency_id")]
public int? Id { get; set; }
[Column("name")]
public string Name { get; set; }
}
[Table("user")]
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("user_id")]
public int? Id { get; set; }
[Column("username")]
public string Username { get; set; }
[Column("agency_id")]
public int AgencyId { get; set; }
[ForeignKey("AgencyId")]
public Agency Agency { get; set; }
}
[Table("property")]
public class Property
{
[Key] …
Run Code Online (Sandbox Code Playgroud) c# state asp.net-core-mvc asp.net-core-webapi asp.net-core-1.1
显然,.Net Core 2.1 现在支持视图。我想知道是否可以使用 Pomelo 来构建视图,如果可以,语法是什么?我在视图中尝试了“表”语法,但没有用:
dotnet ef dbcontext scaffold "Server=myserver.com;Database=myDatabase;User=userame;Password=password;" "Pomelo.EntityFrameworkCore.MySql" -t personsView -o models
Run Code Online (Sandbox Code Playgroud)
它运行,但它只生成一个 dbContext - 它不生成模型。
我正在使用 Pomelo 2.1.1 和 Visual Studio 2017 (15.7.5)。我的项目是 .Net Core 2.1 Web API。在后端,我有 MySQL Server 5.6.30。
mysql .net-core-2.1 ef-core-2.1 pomelo-entityframeworkcore-mysql
我有一个使用 Entity Framework Core 的 ASP .Net Core 2.2 Web API。它使用最新版本的 Pomelo 连接到 MySQL 数据库。当我尝试执行 Linq 函数“TakeLast”时,如下所示:
var messages = dbContext.Messages
.TakeLast(5)
.ToList();
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
抛出异常:Remotion.Linq.dll 中的“System.NotSupportedException”
System.NotSupportedException:无法解析表达式“值(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[PropWorx.API.Models.Message])。TakeLast(__p_0)”:方法“System.Linq.Queryable”的重载目前不支持“.TakeLast”。
Ling 功能“Take”有效,但“TakeLast”无效。这是MySql的限制吗?还是柚子?有什么解决办法吗?
谢谢
我正在努力理解一些事情。我有一个 .Net Core 2.2 Web API,带有 MySQL 8 数据库,并使用 Pomelo 库连接到 MySQL 服务器。
我有一个 PUT 操作方法,如下所示:
// PUT: api/Persons/5
[HttpPut("{id}")]
public async Task<IActionResult> PutPerson([FromRoute] int id, Person person)
{
if (id != person.Id)
{
return BadRequest();
}
_context.Entry(person).State = EntityState.Modified;
try
{
_context.SaveChanges(); // Works
// await _context.SaveChangesAsync(); // Doesn't work
}
catch (DbUpdateConcurrencyException)
{
if (!PersonExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
Run Code Online (Sandbox Code Playgroud)
根据我在上面的代码片段中的评论,当我调用_context.SaveChanges()时,它起作用(即它更新 MySQL 数据库中的相关记录,并返回 1),但是当我调用wait _context.SaveChangesAsync()时,它不起作用(它不更新记录,并且返回 0)。它不会抛出异常或任何异常 - …
我最近将我的 ASP .Net Core 2.2 Web API 升级到了 .Net Core 3.0。现在,当我从 Visual Studio 2019 社区(安装了最新更新)中发布到 Azure 应用服务时,我收到以下消息:
在 propworx-api-san 上启动 PropWorx.API 时出现问题。您的应用程序需要 .NET Core 3.0.0 运行时,但 Microsoft Azure 应用服务仅支持以下版本:2.1.12、2.1.13、2.2.6、2.2.7 和 3.0.0。
然而,该 API 在部署后似乎可以正常工作。这是我应该关心的事情吗?
deployment azure asp.net-web-api azure-app-service-envrmnt asp.net-core-3.0
在EF Core 1.1中,我可以获得具有特定ClientId的所有用户:
var users = _context.Users.Where(u => u.ClientId == 1)
Run Code Online (Sandbox Code Playgroud)
但是,是否可以返回具有属于集合的ClientId的所有用户?就像是
var users = _context.Users.Where(u => u.ClientId IN (1, 2, 3, 4, 5))
Run Code Online (Sandbox Code Playgroud)
?
asp.net-core ×4
c# ×3
mysql ×2
pomelo-entityframeworkcore-mysql ×2
.net ×1
.net-core ×1
asp.net-core-hosted-services ×1
asp.net-mvc ×1
azure ×1
deployment ×1
ef-core-2.1 ×1
ef-core-2.2 ×1
linq ×1
signalr ×1
sql ×1
state ×1
webapi ×1
wpf ×1