【发布时间】:2022-08-06 18:47:35
【问题描述】:
我应用了迁移,可以在 HeidiSQL 中看到表,但在尝试 SaveChanges() 时仍然出现错误
System.InvalidOperationException: \'A relational store has been configured without specifying either the DbConnection or connection string to use.\'
加载指令.cs
using System.ComponentModel.DataAnnotations;
namespace NectarWarehouseAppServer.Models
{
public class LoadingInstruction
{
[Key]
public int? LoadingInstructionRecordId { get; set; }
public string RouteId { get; set; }
public string SalesOrderId { get; set; }
public int? LoadersTeamId { get; set; }
}
}
加载指令上下文.cs
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using NectarWarehouseAppServer.Models;
namespace NectarWarehouseAppServer.Models
{
public class LoadingInstructionContext : DbContext
{
public LoadingInstructionContext(DbContextOptions<LoadingInstructionContext> options)
: base(options)
{
}
public LoadingInstructionContext()
{
}
public DbSet<LoadingInstruction> LoadingInstruction { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Configuring the one to many relationship
modelBuilder.Entity<LoadingInstruction>()
.HasKey(e => e.LoadingInstructionRecordId);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(\"appsettings.json\")
.Build();
var connectionString = configuration.GetConnectionString(\"ConnectionString\");
optionsBuilder.UseMySql(ServerVersion.AutoDetect(connectionString),
optionsBuilder => optionsBuilder.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null));
}
}
}
}
ILoadingInstructionService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NectarWarehouseAppServer.Models
{
public interface ILoadingInstructionService
{
Task<int> Delete(int id);
Task<IEnumerable<LoadingInstruction>> FindAll();
Task<LoadingInstruction> FindOne(int id);
Task<int> Insert(LoadingInstruction loadingInstruction);
Task<int> Update(LoadingInstruction loadingInstruction);
}
}
MariaDbContext.cs
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
using NectarWarehouseAppServer.Models;
namespace NectarWarehouseAppServer.Models
{
public partial class MariaDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public MariaDbContext(DbContextOptions<MariaDbContext> options)
: base(options)
{
}
public virtual DbSet<Users> Users { get; set; }
public virtual DbSet<LoadingInstruction> LoadingInstruction { get; set; }
}
}
应用设置.json
{
\"Logging\": {
\"LogLevel\": {
\"Default\": \"Information\",
\"Microsoft.AspNetCore\": \"Warning\"
}
},
\"ConnectionStrings\": {
\"ConnectionString\": \"server=localhost;port=3306;database=****;uid=****;pwd=****;\"
}
}
程序.cs
using Microsoft.EntityFrameworkCore;
using NectarWarehouseAppServer.Models;
using NectarWarehouseAppServer.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContextPool<MariaDbContext>(opt =>
{
opt.UseMySql(builder.Configuration.GetConnectionString(\"ConnectionString\"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString(\"ConnectionString\")),
builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
builder.Services.AddControllers();
builder.Services.AddDbContext<CustomerContext>(opt =>
{
opt.UseMySql(builder.Configuration.GetConnectionString(\"ConnectionString\"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString(\"ConnectionString\")),
builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
builder.Services.AddScoped<IUsersService, UsersService>();
builder.Services.AddControllers();
builder.Services.AddDbContext<UsersContext>(opt =>
{
opt.UseMySql(builder.Configuration.GetConnectionString(\"ConnectionString\"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString(\"ConnectionString\")),
builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
builder.Services.AddControllers();
builder.Services.AddDbContext<NectarWarehouseAppServer.Models.RouteContext>(opt =>
{
opt.UseMySql(builder.Configuration.GetConnectionString(\"ConnectionString\"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString(\"ConnectionString\")),
builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
builder.Services.AddScoped<ILoadingInstructionService, LoadingInstructionService>();
builder.Services.AddControllers();
builder.Services.AddDbContext<LoadingInstructionContext>(opt =>
{
opt.UseMySql(builder.Configuration.GetConnectionString(\"ConnectionString\"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString(\"ConnectionString\")),
builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
});
});
builder.Services.AddConnections();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseSwagger();
//app.UseSwaggerUI(c => c.SwaggerEndpoint(\"/swagger/v1/swagger.json\", \"TodoApi v1\"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
我尝试了几种不同的连接字符串配置,例如使用 \'user\' 而不是 \'uid\' 和 \'password\' 而不是 \'pwd\',省略端口等。
这是我得到错误的地方
try
{
await using (LoadingInstructionContext dbContext = new LoadingInstructionContext())
{
foreach (var loading in loadingInstructions){
LoadingInstruction loadingInstruction = new LoadingInstruction();
loadingInstruction.RouteId = \"Route1\";
loadingInstruction.SalesOrderId = \"Order1\";
loadingInstruction.LoadersTeamId = 0;
dbContext.Add(loadingInstruction);
dbContext.SaveChanges();
}
LoadingInstruction loadingInstruction = new LoadingInstruction();
dbContext.Add(loadingInstruction);
dbContext.SaveChanges();
}
return Ok();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return BadRequest(ex.ToString());
}
标签: c# mysql entity-framework mariadb pomelo-entityframeworkcore-mysql