Specifying ASP.NET MVC attributes for auto-generated data models
Posted
by
Lyubomyr Shaydariv
on Stack Overflow
See other posts from Stack Overflow
or by Lyubomyr Shaydariv
Published on 2010-12-26T18:51:10Z
Indexed on
2010/12/26
18:53 UTC
Read the original article
Hit count: 460
Hello to everyone.
I'm very new to ASP.NET MVC (as well as ASP.NET in general), and going to gain some knowledge for this technology, so I'm sorry I can ask some trivial questions. I have installed ASP.NET MVC 3 RC1 and I'm trying to do the following.
Let's consider that I have a model that's completely auto-generated from a table using the "LINQ to SQL Classes" template in VS2010. The template generates 3 files (two .cs files and one .layout file respectively), and the generated partial class is expected to be used as an MVC model. Let's also consider, a single DB column, that's mapped into the model, may look like this:
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
get {
return this._Name;
}
set {
if ( (this._Name != value) ) {
// ... generated stuff goes here
}
}
}
The ASP.NET MVC engine also provides a beautiful declarative way to specify some additional stuff, like RequiredAttribute
, DisplayNameAttribute
and other nice attributes. But since the mapped model is a purely auto-genereated model, I've realized that I should not change the model manually, and specify the fields like:
[Required]
[DisplayName("Project name")]
[StringLength(128)]
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_Name", DbType = "VarChar(128)")]
public string Name {
...
though this approach works perfectly... until I change the model in the DBML-designer removing the ASP.NET MVC attributes automatically. So, how do I specify ASP.NET MVC attributes for the DBML models and their fields safely?
Thanks in advance, and Merry Christmas.
© Stack Overflow or respective owner