【问题标题】:System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. 3System.ArgumentException:初始化字符串的格式不符合从索引 0 开始的规范。3
【发布时间】:2018-06-07 06:56:30
【问题描述】:

我正在尝试制作一个 .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:

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);
    }

我尝试了自己的配置文件,但结果是一样的。怎么了?

【问题讨论】:

    标签: c# asp.net-web-api .net-core


    【解决方案1】:

    此错误表明您的连接字符串不正确。

    “pooling”这个词有错别字,试试下面的连接字符串:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      下面是正确的代码。我的错误消失了。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-04
        • 1970-01-01
        • 2011-08-24
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多