I have this class:
public class MyClass {
[Column(Name="StoredColumn", DbType="int")]
public int Stored;
public int ForDisplay {
get { return Stored * 1000; }
}
}
The point is ForDisplay is not to be stored in the database – I just need it for more convenient code.
I try to run an SQL query that returns a rowset and get this InvalidOperationException:
Cannot assign value to member ‘ForDisplay ‘. It does not define a setter.
I don’t want ForDisplay to be touched by Linq-To-Sql. How do tell Ling-To-Sql to not touch it?
You don’t need to tell Linq to Sql that it should not touch the “ForDisplay” property. Normally, all properties that are not specifically marked with the
Columnattribute are treated as transient part of your application logic and are not persisted or retrieved from the database.However, if you want to retrieve some records from the database via a stored procedure, say
GetMyClasses, then the designer will create a new classGetMyClassesResult. If you look at the generated code, then you will notice that the class is not decorated with theTableattribute. In that case the mapper assumes that every property must be assigned. If there is a property without corresponding value in the query then the mapper will try to set the property to default(T), but as there is no setter, the exception will be thrown.Anyway, to make a long story short, just adding
to your class should fix the issue.