Is there a way to use Linq to SQL without adding a .dbml file?

I work with Linq to SQL and always add a .dbml file that acts as a bridge between my application and the SQL server. I'm just wondering if there is a way to use Linq to SQL without using. dbml file? I came across a link here for the same thing, but this is not clear, can someone please shed some light on this.

+4
source share
2 answers

Yes of course. In fact, when I first studied LINQ-to-Sql, the automatically generated code using the boiler plate of the dbml files was actually too large to be digested at the beginning. Therefore, I began to design my POCO classes myself one at a time, and then began to study LINQ query records. Here is a quick example to get started:

I created the database "businessLinqToSql" with one table named Customer. Use the below mentioned SQL script:

USE [businessLinqToSql]
GO

/****** Object:  Table [dbo].[Customer]    Script Date: 06/17/2016 11:28:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Customer](
    [ID] [int] NOT NULL,
    [Name] [nchar](30) NOT NULL,
    [Address] [nchar](30) NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Create a C # console application and add a code file named Customer.cs as shown below. This will be your POCO class with some declarative attributes, which gives a hint regarding the database schema to the LINQ-to-SQL provider:

[Table(Name="Customer")]
public class Customer
{

    [Column(IsPrimaryKey = true)]
    public int ID { get; set; }

    [Column] 
    public string Name { get; set; }

    [Column]
    public string Address { get; set; }

}

Then you can write below LINQ query in your main function:

private static void LinqToSql()
        {
            DataContext dataContext = new DataContext("data source=.;initial catalog=businessLinqToSql;integrated security=True;MultipleActiveResultSets=True");
            Table<Customer> customers = dataContext.GetTable<Customer>();

            IQueryable<string> query = from c in customers
                                       where c.Name.Length > 5
                                       orderby c.Name.Length
                                       select c.Name.ToUpper();
            foreach (string name in query) Console.WriteLine(name);
        }
+5
source

, (context.TableName), .

, , , ( Entity Framework).

, [ , dbml ]

TableAttribute. , dbml. . .

:

[Table] 
public class Customer
{
   [Column(IsPrimaryKey=true)]  
   public int ID;
   [Column]                     
   public string Name;
}

public class DemoDataContext : DataContext
{
  public DemoDataContext (string cxString) : base (cxString) { }

  public Table<Customer> Customers { get { return GetTable<Customer>(); } }
  public Table<Purchase> Purchases { get { return GetTable<Purchase>(); } }
}

:

var db = new DataContext();
var customers = db.GetTable<Customer>();
var query = customers.Where (c => c.Name.StartsWith ("a"));
+2

All Articles