Visual Web Developer 2008 won't auto generate classes from Linq
I'm new to Linq and Visual web developer 2008 Express. I have read some posts here and Scott Guthrie's on setting up Linq, but I'm a little stuck because the classes aren't auto-generating as I thought they would. I have setup a database with a simple table (with the Primary Key as the ID, and it is set to auto-increment) plus a few other columns, and dragged and dropped it on to the DBML Linq designer pane. The table appears in the window, but no classes are auto-generated. When I right click on the table and select "View Code", DataClasses.cs
is displayed, but I only see a part开发者_高级运维ial class with no methods or properties.
Isn't Linq supposed to do this, or have I (quite likely) missed the point completely? Or is this functionality not available in Visual Web Developer 2008 Express?
No, you're not missing anything. That is exactly how it should work. Go to solution explorer, expand the dbml, and double-click on whatever.designer.cs. Down at the bottom, you'll see ...
[Table(Name="dbo.YourTable")]
public partial class YourTable: INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
... and all of your properties.
As the other answerers have already pointed out, you do get your code generated - into a single (modelname).designer.cs file.
This may or may not be what you expected and wanted - if you can live with it - perfect!
If not, there's at least one set of Code generation templates out there called PLINQO which are based on CodeSmith, the well known code generator. Those templates allow you to do a lot of things standard Linq-to-SQL doesn't support:
- generate classes for each table into their own, single file in a user-definable folder
- actually update your DBML model and all associated generated classes if the underlying database changes
- adds a "(entityname)Manager" class to manage entities of a given type (like Customer etc.)
All in all, it's quite a neat solution to handle Linq-to-SQL code generation. Excellent stuff - recommended!
Marc
As mentioned, LINQ-To-SQL will not actually generate individual class files for you - if you go and write some code that references a class for one of your tables, you will find that it is there...
精彩评论