6

I am trying to make a .NET Core Web API but I couldn't find out a solution for my problem.

Full error is

[ERR] An exception occurred in the database while iterating the results of a query for context type '"MailAlertWebApiWithEF.Data.AlertContext"'." ""System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

I get that configuration codes from an existing database with

Scaffold-DbContext "server=.;ConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBModels -force -v

My configuration is:

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

My context is:

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

My ConnectionStrings inside at appsettings.json:

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

When I start at debug mode the API gets stuck inside the engine layer.

My Engine and 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);
    }

I tried my own configuration file but the result is the same. What is wrong?

0

2 Answers 2

10

This error indicates that your connection string is not correct.

There is a typo in the word "pooling", try with the following connection string:

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

If still not working, make sure the server and database values are OK.

Sign up to request clarification or add additional context in comments.

Comments

3

I also experienced this same error, but the problem is not with the connection string, but due to some confusions like below:

In Startup.cs file, I have added the below setting which is wrong:

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

Below is the right code. My error has gone.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.