views:

446

answers:

2

I go in and add a new "linq to sql classes" in Visual Studio and then go and drag a table from Database Explorer to the new DBML and the name of the new class is no longer plural. What if I still want it to be plural? If I drag a table that isn't plural I get a bunch of compile errors about how there is already a type definition for all of the fields in the table I put on the dbml layout. If I then make the class name plural for the table that wasn't plural in the dbml layout by clicking on the name and changing it then no more compile errors.

+6  A: 

You can single click on the tablename in the dbml table and rename it. Or you can select the table in the DBML and go to the properties window. There you can change the name, and the table it hooks up to.

One way to avoid the name conflict is to designate namespaces accordingly. For example, you can place your DBML file in a subfolder in your project and assign it something like DataAccess. Therefore, when you map to the Ling2Sql class, you'd do DataAccess.Customer and you can avoid the conflict with Customer, since it lives somewhere else.

As a tidbit, Linq2Sql by default makes tables non-plural. It's based off of a convention. For example, a table Customers has many customers. When you instantiate an object, you're looking at a single Customer, not the table. The object is essentially being mapped to a row in the Customers table, thus it becomes singular.

MunkiPhD
I'll try that subfolder thing but I did try to rename the class like you said and got a bunch of compile errors about type conflicts like I did with the "Customer" table. If I drag over a table called "Customer" I don't even need to change anything else and I get a bunch type conflicts.
tooshel
A: 

I just figured out what was happening. After the "move it to a new folder" suggestion I tried that and it worked. But it didn't make any sense as to why it worked because if there were type conflicts there should have still been the same conflicts in the new folder but I had none of the problems I was having before and I could rename things anything I wanted after moving to a new folder and there were no conflicts. Renaming things after dragging it over is a bad idea because if the database schema changes and you want to update your classes you have to drag the table back over and then make the changes manually again.

ANYWAY, the problem is that before I made the "linq to sql classes" I made a "ADO.NET Entity Data Model". That's what was causing all the name conflicts. But the suggestion about moving it to a new folder was awesome and led me to the final solution!

tooshel
Yea -- that's a pet peeve of mine with Linq2Sql. If your schema changes a lot, it can be troublesome. ESPECIALLY if you're prototyping. I recently saw a demo of cool stuff that Rob Conery and his contributors are doing on SubSonic. It seems like it's going to be a fast prototyping and 'hit the ground' running tool. I don't know if it suits your project, but you should take a look at it. The SimpleRepository and ActiveRecord implementations look very VERY promising.
MunkiPhD
And the reason the name conflict disappeared when you moved it to a different folder is that Linq2Sql generates a new namespace for the datacontext every time you update the schema in the DBML. SO by migrating to a different folder and changing it, it attached that folder name onto the namespace -- thus removing the conflicts since I'm assuming you had a lot of classes living in the same namespace.
MunkiPhD
I was just looking thought the NerdDinner code and noticed they changes a bunch of stuff in the linq to SQL classes and it seems crazy to me given that the database schema changes would be hard to integrate back into the classes. And I'll look at Rob's work . . . though he's mostly all about the next version now and I certainly don't want to mess up my MVC 1.0 install with an MVC beta!
tooshel