views:

111

answers:

3

According to Microsoft:

http://msdn.microsoft.com/de-de/library/system.data.linq.mapping.columnattribute.expression.aspx

It's possible to add expression to the Linq-to-SQL Mapping.

But how to configure or add them in Visual Studio in the Designer? Problem, when I add it manual to thex XYZ.designer.cs it on change it will be lost.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4927
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

This is generated:

   [Column(Name="id", Storage="_id", DbType="Int")]
   public System.Nullable<int> id
   {
    ...

But i need something like this

[Column(Name="id", Storage="_id", DbType="Int", Expression="Max(id)")]
public System.Nullable<int> id
{
  ...

Thanks.

A: 

Edit: mmmm you edited your question, so probably my answer is not gonna help you so much, but you might be able to do this anyway with your 'extended' question :D

I do this by adding another class file to the project, give them the same name as the object from LinQ-to-SQL you want to extend and define it as partial.

for example, if you have a table called Files, the object File will be created for you by L2S. If you then create a file (with the same namespace as your DataContext object), and make it partial, like this:

public partial class File
{
}

You can just add properties, methods, etc. From within this class, you also have direct access to the properties of the 'other' File class.

Wim Haanstra
Right. But I don't want add or reimplement in partial class more meothods for get and set, just add an annotation to a generated class. Do you have an example how to add annotation on method? [Column(Expression="Max(id)")] public System.Nullable<int> id { ...Isn't compilable:
csharpnoob
Hmmm no. Dont think I can help you there. Sorry.
Wim Haanstra
A: 

It's a little klugy, but in your linq2sql designer, rename the field from 'id' to 'xid' (or anything else) and change its accessibility to internal.

then, in another file, start another partial class, like Wim Haanstra showed, and create a new property called 'id', add all the attributes you want, and in the get & set, just map it to and from the original property, now called 'xid'.

it would look something like this:

public partial class File
{
  public int? id
  {
     get { return xid; }
     set { xid = value; }
  }
}

this is more commonly done to map fields in the database to a different type in the object, e.g. an int in the DB to an enum in the object, a byte/smallint/etc. in the DB, a boolean in the object. or to add attributes, like [DataMember] to the property.

Mike Jacobs
thanks. i already used this kind of mapping, but then linq-to-sql can't produce proper sql anymore. see here http://stackoverflow.com/questions/2757444/problem-with-mapping-casting-linq-to-sql-on-different-types.
csharpnoob
true, when building the actual linq2sql statement, you need to refer to the original fields from the DB, not your created ones. but that's only necessary in the datalayer. outside of it (presentation, business, etc. layers), you can just refer to your created field name.
Mike Jacobs
A: 

According to this article:

http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.expression.aspx

you should use the ColumnAttribute.Expression Property when you use CreateDatabase to define a column as containing computed values. So you should check this article:

http://msdn.microsoft.com/en-us/library/Bb399420%28v=VS.100%29.aspx

Another way is to define expression on your sql server so it'll be mapped by the LINQ designer.

VMAtm
i guess your are right, this is the only way having it as default value. thanks, but i thing i'm going to put it int the partial constructor.
csharpnoob