You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have used SqlKata's [Key] attribute without the name specified for years without any errors complaining about it, even with SqlKata.Execution.
However CsvHelper seems to be not happy with these nameless keys. When used together, some exceptions are thrown:
Unhandled exception. CsvHelper.WriterException: An unexpected error occurred. See inner exception for details.
IWriter state:
Row: 1
Index: 0
HeaderRecord:
1
---> System.ArgumentNullException: Value cannot be null. (Parameter 'name')
at SqlKata.ColumnAttribute..ctor(String name)
at SqlKata.KeyAttribute..ctor(String name)
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeFieldInfo field, RuntimeType caType)
at System.Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
at CsvHelper.Configuration.ClassMap.ApplyAttributes(MemberMap memberMap)
at CsvHelper.Configuration.ClassMap.AutoMapMembers(ClassMap map, CsvContext context, LinkedList`1 mapParents, Int32 indexStart)
at CsvHelper.Configuration.ClassMap.AutoMap(CsvContext context)
at CsvHelper.CsvContext.AutoMap(Type type)
at CsvHelper.CsvWriter.WriteHeader(Type type)
at CsvHelper.CsvWriter.WriteHeaderFromType[T]()
at CsvHelper.CsvWriter.WriteRecords[T](IEnumerable`1 records)
--- End of inner exception stack trace ---
at CsvHelper.CsvWriter.WriteRecords[T](IEnumerable`1 records)
at Program.<<Main>$>g__WriteCsv|0_0[T](IEnumerable`1 values) in C:\Users\user\source\repos\sln1\proj1\Program.cs:line 26
at Program.<Main>$(String[] args) in C:\Users\user\source\repos\sln1\proj1\Program.cs:line 13
To Reproduce
usingCsvHelper;usingCsvHelper.Configuration;usingSystem.Globalization;MyRecord[]records=[new(){Id=1,Value="A"},new(){Id=2,Value="B"},];varcsv=WriteCsv(records);Console.WriteLine(csv);staticstringWriteCsv<T>(IEnumerable<T>values){usingvarwriter=newStringWriter();varconfig=newCsvConfiguration(CultureInfo.InvariantCulture){MemberTypes=MemberTypes.Properties|MemberTypes.Fields};usingvarcsv=newCsvWriter(writer,config);csv.WriteRecords(values);returnwriter.ToString();}structMyRecord{[SqlKata.Key]// Error! // [SqlKata.Key(nameof(Id))] or any other value for `name` is REQUIRED, when using with CsvHelperpublicintId;publicstringValue;}
SqlKata expects the parameter to be filled by the compiler (using [System.Runtime.CompilerServices.CallerMemberName]) but when the runtime does its work in GetCustomAttributes() (which apparently involves calling the constructor), it does not know or consider the value substituted by the compiler at the definition site.
Describe the bug
I have used SqlKata's
[Key]
attribute without the name specified for years without any errors complaining about it, even withSqlKata.Execution
.However CsvHelper seems to be not happy with these nameless keys. When used together, some exceptions are thrown:
To Reproduce
Expected behavior
Screenshots
N/A
Additional context
An issue for the same bug is also created in SqlKata's issue tracker.
The text was updated successfully, but these errors were encountered: