简体   繁体   中英

Adding attribute to auto generated Linq to SQL class

I have a dbml file and dropped a Products table onto it. I now have access to the auto generated Products class file which is great.

I would like to export a CSV file's content and save it to this table, so i decided to go with Linq to CSV http://aspnetperformance.com/post/LINQ-to-CSV-library.aspx

It seems in order for the file to be read, a property needs to be decorated with an attribute ie

<CsvColumn(Name:= "ProductName", FieldIndex:= 1)>

But when i use the Products class it returns an error that the column name cant be found (i know this, as i created a different project which read the file without issues once the above attribute was added)

So i created a partial class and added the attribute:

<Metadatatype(GetType(ProductsMetadata))> _
Partial Public Class Products

End Class

Friend Class ProductsMetadata

<CsvColumn(Name:= "ProductName", FieldIndex:= 1)>
Public Property ProductName As String

End Class

However this still didnt work. The reason why i need to add an attribute is so when i save it i can pass in the Products object into the save method

myDataContext.Products.InsertOnSubmit(ProductObject)
myDataContext.SubmitChanges()

Is it possible to add attributes in the above manner or any other way i could do this?

Not without editing the generated file. Partial classes can be used to add attributes to the type ,and to add members to the type - but you can't add attributes to members in another part of the partial class. It would be nice if we could, I agree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM