Last active
June 21, 2019 21:51
-
-
Save ozkary/6ca2a1b8994486c7f0edb2a41cba3bfd to your computer and use it in GitHub Desktop.
Single JOIN multi statement with embedded SQL Use it when there is no access to create views on the database which is the preferred approach.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static void Main(string[] args) | |
| { | |
| Console.WriteLine("Running a simple multi join statement on EF!"); | |
| var connection = @"Server=.\sqlexpress;Database=dev;Trusted_Connection=True;ConnectRetryCount=0"; | |
| var optionsBuilder = new DbContextOptionsBuilder<TaskContext>(); | |
| optionsBuilder.UseSqlServer(connection); | |
| using (TaskContext context = new TaskContext(optionsBuilder.Options)) | |
| { | |
| string sql = @" select t.taskid,description, | |
| ts.name as status, | |
| tt.name type, | |
| tsv.name as severity, | |
| tp.name as priority | |
| from [bi].Task t (NOLOCK) | |
| inner join [bi].[LK_TaskStatus] ts (NOLOCK) on t.taskstatusid = ts.taskstatusid | |
| inner join [bi].[LK_TaskType] tt (NOLOCK) on t.tasktypeid= tt.tasktypeid | |
| inner join [bi].[LK_TaskSeverity] tsv (NOLOCK) on t.taskseverityid= tsv.taskseverityid | |
| inner join [bi].[LK_TaskPriority] tp (NOLOCK) on t.taskpriorityid= tp.taskpriorityid"; | |
| List<Task> tasks = context.Tasks.FromSql<Task>(sql).ToList(); | |
| foreach(var task in tasks) | |
| { | |
| Console.WriteLine($" {task.TaskId}, {task.Description}, {task.Priority}, " + | |
| $"{task.Status}, {task.Type}, {task.Severity}," + | |
| $" {task.Priority}"); | |
| } | |
| } | |
| Console.WriteLine("Press any key to exit"); | |
| Console.ReadLine(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TaskContext : DbContext | |
| { | |
| public TaskContext(DbContextOptions<TaskContext> options) | |
| : base(options) | |
| { } | |
| public DbSet<Task> Tasks { get; set; } | |
| } | |
| public class Task | |
| { | |
| public int TaskId { get; set; } | |
| public string Description { get; set; } | |
| public string Status { get; set; } | |
| public string Severity { get; set; } | |
| public string Priority { get; set; } | |
| public string Type { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment