Error when adding Linq to Sql class it says "A file with name studentdb.dbml.layout already exist."

Habibullah 10 Reputation points
2023-08-30T11:18:56.52+00:00

Whenever I'm trying to add LINQ to SQL class as a new item in visual studio 2022, then every time it shows an error by saying "A file name studentdb.dbml.layout already exist. Please give us a unique name to the item you are adding or delete the existing one." But for every new name unlike studentdb always it shows the error and if it is normal then it supposed to add "system.data.linq" in the reference section but it didn't add that, but it adds designer.cs file under .dbml file but in the .designer.cs file .linq isn't recognized.
I have added LINQ to SQL as individual component. Please help me.Untitled picture

Developer technologies Visual Studio Other
SQL Server Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-10-10T21:58:43.42+00:00

    The following was done in VS2022, .net core 7.

    I added a LINQ to SQL class, got the same errors but the files were created correctly. Next added netstandard-System.Data.Linq NuGet package. Then rebuild the project, added a few lines of code to validate I could read data and worked as expected.

    If you are going to continue using these classes report the initial error to Microsoft using the feedback button, top right corner of Visual Studio.

    Example code:

    NorthdbDataContext context = new NorthdbDataContext();
    var categories = context.Categories.ToList();
    foreach (var category in categories)
    {
        Debug.WriteLine($"{category.CategoryID,-3}{category.CategoryName}");
        foreach (var prod in category.Products.OrderBy(p => p.ProductName))
        {
            Debug.WriteLine($"   {prod.ProductID,-4}{prod.ProductName}");
        }
    }
    

    Diagram

    Diagram

    Notes:

    • LINQ to SQL is no longer under development
    • Can have only a 1:1 mapping relationship with a database table or view.
    • Use the Entity Framework Core for complex mapping
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.