Can I use Linq-SQL without drag and drop?

If I want to use Linq-SQL, I also need to drag the DB table onto the constructor surface to create entity classes.

I always like the full control in my application and I don't like the classes created by dotnet.

Is it possible to provide this connection between Linq and DB using my own Entity Layer Layer classes?

How can i do this?

+5
source share
3 answers

You can easily write your classes using Linq-to-SQL - you just need to draw your classes using some attributes.

For example, this is a very simple table that is in one of my projects, and it works fine with Linq-to-SQL:

[Table(Name = "Categories")]
public class Category : IDataErrorInfo
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }
    [Column] public string Name { get; set; }
    [Column] public string ExtensionString { get; set; }
}

, ( ).

:

class CategoryRepository : ICategoryRepository
{
    private Table<Category> categoryTable;
    public CategoryRepository(string connectionString)
    {
        categoryTable = (new DataContext(connectionString)).GetTable<Category>();
    }
}

, -, , , . , 100% , Linq-to-SQL.

Pro ASP.NET MVC Framework, .

, Linq-to-SQL , .

+7

, SqlMetal.exe.

, , Persistence Notorance, , L2S - LINQ to Entities .NET 4...

- SqlMetal.exe - , , .

+1

I have several CodeProject tutorials that go through this, including how to handle relationships (M: M, 1: M, M: 1) in the OO way and synchronize them, you make updates:

LINQ Tutorial: Mapping Tables to Objects

LINQ Tutorial: Add / Update / Delete Data

+1
source

All Articles