简体   繁体   中英

Generated C# LINQ to SQL Classes

Visual Studio will generate the codes itself in DataClasses.designer.cs after I drag a table from the Server Explorer to DataClasses.dbml.

If the table's name end with a 's', such as Books , VS will remove the 's' In the class name and keep the 's' with the property name:

public System.Data.Linq.Table<Book> Books
{
    get
    {
        return this.GetTable<Book>();
    }
}

If the table does not end with a 's', such as Book , VS will keep the class name and add a 's' to the property name:

public System.Data.Linq.Table<Book> Books
{
    get
    {
        return this.GetTable<Book>();
    }
}

However, my colleague's VS just keeps the names:

Book: public System.Data.Linq.Table<Book> Book
Books: public System.Data.Linq.Table<Books> Books

We work on a same project with our own pc. If drag a new table to DataClasses.dbml and save, my VS will change the his old codes:

public System.Data.Linq.Table<Book> Book

to

public System.Data.Linq.Table<Book> Books

and all his codes such as

DataClassesDataContext db = new DataClassesDataContext();
var book = db.Book;

will be error, because now it must be:

var book = db.Books;

How can I solve this problem?

By default, when you drag database objects that have names ending in s or ies from Server Explorer/Database Explorer onto the LINQ to SQL Tools in Visual Studio, the names of the generated entity classes are changed from plural to singular. This is done to more accurately represent the fact that the instantiated entity class maps to a single record of data. For example, adding a Customers table to the O/R Designer results in an entity class named Customer because the class will hold data for only a single customer.

How to: Turn pluralization on and off (O/R Designer):

https://msdn.microsoft.com/en-us/library/bb384507.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM