Skip to content

Instantly share code, notes, and snippets.

@gorauskas
Created April 28, 2011 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorauskas/947347 to your computer and use it in GitHub Desktop.
Save gorauskas/947347 to your computer and use it in GitHub Desktop.
LINQ to SQL Association Mapping
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace MyNS{
static void Main(string[] args) {
JobsDataContext db = new JobsDataContext("Data Source=ServerName;Initial Catalog=MSDB;Integrated Security=SSPI;");
Table<SysJobStep> primary_sysjobs = primary_db.SysJobSteps; //error occurs here
List<string> primary_sysjobstep_query = (from sjs in primary_sysjobs select sjs.SysJob.Name + "." + sjs.Name).ToList();
}
public class JobsDataContext : DataContext {
public Table<SysJob> SysJobs;
public Table<SysJobStep> SysJobSteps;
public JobsDataContext(string connection) : base(connection) { }
}
[Table(Name = "SysJobs")]
public class SysJob {
private string _jobid;
private string _name;
private EntitySet<SysJobStep> _SysJobSteps;
public SysJob() {
this._SysJobSteps = new EntitySet<SysJobStep>();
}
[Column(Name = "job_id", Storage = "_jobid", CanBeNull = false,
DbType = "uniqueidentifier not null")]
public string JobId {
get { return this._jobid; }
set { this._jobid = value; }
}
[Column(Name = "name", Storage = "_name", CanBeNull = false)]
public string Name {
get { return this._name; }
set { this._name = value; }
}
[Association(Storage = "_SysJobSteps", OtherKey = "JobId")]
public EntitySet<SysJobStep> SysJobSteps {
get { return this._SysJobSteps; }
set { this._SysJobSteps.Assign(value); }
}
}
[Table(Name = "SysJobSteps")]
public class SysJobStep {
private string _jobid;
private int _stepid;
private string _name;
private EntityRef<SysJob> _job;
public SysJobStep() {
this._job = new EntityRef<SysJob>();
}
[Column(Name = "job_id", Storage = "_jobid", CanBeNull = false, DbType = "uniqueidentifier not null", IsDbGenerated = true)]
public string JobId {
get { return this._jobid; }
set { this._jobid = value; }
}
[Association(Storage = "_job", ThisKey = "JobId")]
public SysJob SysJob {
get { return this._job.Entity; }
set { this._job.Entity = value; }
}
[Column(Name = "step_id", Storage = "_stepid", CanBeNull = false, DbType = "int not null")]
public int StepId {
get { return this._stepid; }
set { this._stepid = value; }
}
[Column(Name = "name", Storage = "_name", CanBeNull = false)]
public string Name {
get { return this._name; }
set { this._name = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment