Error Guid should contain 32 digits with 4 dashes

asp.net c# entity-framework-core guid postgresql

Question

Got an error

"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)"

The GUID input is "68f0eaed-189e-4c65-8cf8-475539d6f21b"


    context.Countries.AddRange(GetCountryData(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "csv", "country.csv")));

    await context.SaveChangesAsync();


    public List<Data.Country> GetCountryData(string filePath)
    {
        string csvRead = File.ReadAllText(filePath);
        string[] csvFileRecord = csvRead.Split('\n');
        List<Data.Country> dataRecordList = new List<Data.Country>();

        foreach (var row in csvFileRecord.Skip(1))
        {
            if (!string.IsNullOrEmpty(row))
            {
                var cells = row.Split(',');
                var dataRecord = new Data.Country
                {
                    Id = Guid.Parse(cells[0]),
                    Code = cells[1],
                    Name = cells[2]
                };
                dataRecordList.Add(dataRecord);
            }
        }

        return dataRecordList;
    }

CSV file

"id","code","name"
"68f0eaed-189e-4c65-8cf8-475539d6f21b","AX","Ã…land Islands"
"76cf600f-7bf6-40fb-8803-142eac60f3dd","AL","Albania"
...

EDITED Updated code, it now gets the correct GUID value yet an error still occurs

My input "68f0eaed-189e-4c65-8cf8-475539d6f21b"

fail: MyApp.ContextSeed[0]
      Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT CASE
          WHEN EXISTS (
              SELECT 1
              FROM public.country AS c)
          THEN TRUE::bool ELSE FALSE::bool
      END
1
-2
6/16/2018 12:29:48 PM

Accepted Answer

I suggest to change the code to the following:

public List<Data.Country> GetCountryData(string filePath)
    {
        string csvRead = File.ReadAllText(filePath);
        string[] csvFileRecord = csvRead.Split('\n');
        List<Data.Country> dataRecordList = new List<Data.Country>();

        foreach (var row in csvFileRecord.Skip(1))
        {
            if (!string.IsNullOrEmpty(row))
            {
                var cells = row.Split(',');
                var dataRecord = new Data.Country
                {
                    Id = Guid.Parse(cells[0].Replace("\"", "")),
                    Code = cells[1].Replace("\"", ""),
                    Name = cells[2].Replace("\"", "")
                };
                dataRecordList.Add(dataRecord);
            }
        }

        return dataRecordList;
    }

With the information you just added, it is clear that you are trying to convert a string, that is not a Guid, to a Guid. Note the new indexes at cells. Also csvFileRecord.Skip(1); didn't skip anything; now it should work, because a new IEnumerable is returned in the foreach's head. Also note that you will need to remove the " characters from the cells!

1
6/16/2018 12:37:56 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow