参数异常:初始化字符串的格式不符合规范,从索引0开始。

5
我正在尝试创建一个.NET Core Web API,但是我找不到解决我的问题的方法。
完整错误如下:
[ERR] 在查询上下文类型““MailAlertWebApiWithEF.Data.AlertContext””的结果时,数据库中发生了异常。 “System.ArgumentException: 初始化字符串的格式不符合从索引0开始的规范。”
我使用以下代码从现有数据库获取配置:
Scaffold-DbContext "server=.;ConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBModels -force -v
我的配置如下:
 public class AlertModelConfiguration<T>:BaseEntityModelConfiguration<T,int> where T:Alert
{

    public AlertModelConfiguration(ref ModelBuilder modelBuilder):base(ref modelBuilder)
    {
        modelBuilder.Entity<Alert>(entity =>
        {
            //entity.HasKey(e => e.AlertId);

            entity.Property(e => e.CreateDate).HasColumnType("datetime");

            entity.Property(e => e.DeleteDate).HasColumnType("datetime");

            entity.Property(e => e.Detail)
                .IsRequired()
                .HasMaxLength(2046)
                .IsUnicode(false);

            entity.Property(e => e.ErrorDetail)
                .IsRequired()
                .HasMaxLength(255)
                .IsUnicode(false);

            entity.Property(e => e.Title)
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.UpdateDate).HasColumnType("datetime");
        });
     }
}

我的背景是:

 public class AlertContext:DbContext
  {
    public AlertContext(DbContextOptions<AlertContext> options)
        : base(options)
    {

    }

    public virtual DbSet<Alert> Users { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
       
        new AlertModelConfiguration<Alert>(ref modelBuilder);
       

        base.OnModelCreating(modelBuilder);
    }
}

我的连接字符串在appsettings.json文件中:

ConnectionStrings": {
   "AlertContext": "server=.;database=*****;poling=false;Connect 
 Timeout=60;Integrated Security=SSPI"

当我启动调试模式时,API会在引擎层内卡住。
我的引擎和IEngine:
Task<CustomListEntity<Alert>> GetByIdAsync(int id);

 public  Task<CustomListEntity<Alert>> GetByIdAsync(int id)
    {
        return  CommonOperationAsync(async () =>
         {
             var predicate = PredicateBuilder.True<Alert>();
             predicate = predicate.And(p => p.Id == id);

             

             return new CustomListEntity<Alert>()
             {
                 EntityList = await _alertReqository.GetAll(skip: 0, take: 10, predicate: predicate,
                     orderExpression: null, rowCount: out var count, ascending: false).ToListAsync(),

                 Count = count,
             };
         }, new BusinessBaseRequest()
         {
             MethodBase = MethodBase.GetCurrentMethod()
         }, BusinessUtilMethod.CheckRecordIsExist, GetType().Name);
    }

我尝试了自己的配置文件,但结果仍然相同。错在哪里?
2个回答

9

这个错误表示你的连接字符串不正确。

"pooling" 单词中有一个拼写错误,请使用以下连接字符串进行尝试:

ConnectionStrings": {
         "AlertContext": "server=.;database=*****;pooling=false; 
                 Timeout=60;Integrated Security=SSPI"

如果仍然无法正常工作,请确保服务器和数据库的值正确。

3

我也遇到了同样的错误,但问题不在于连接字符串,而是由于以下一些混淆:

在Startup.cs文件中,我添加了以下设置,这是错误的:

services.AddDbContext<EFCFWithExistingDBContext>(x => x.UseSqlServer(MyDatabaseConnection));

以下是正确的代码。我的错误已经解决了。
services.AddDbContext<EFCFWithExistingDBContext>(x => x.UseSqlServer(Configuration.GetConnectionString("MyDatabaseConnection")));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接