0

I have a three tier ASP.Net Web application. The Data Layer has reference to EF and it contains all the repositories too. So I have installed EF nuget there.

Then I have a service layer to transfer data to and from data layer to views. Issue is, recently I came across a requirement where I need to use DbFunctions.AddDays but I don't want to install the complete EF in service layer for a single query. Is there any way I can achieve the same without installing EF i.e. any other way to achieve the same without using DbFunctions. Query is:

Context.Article.Where(p => EntityFunctions.AddDays(p.StartDate, p.Period) > DateTime.Now);
Garima
  • 401
  • 5
  • 29
  • No actually, I am writing queries in service layer without any issues. I just discovered that for some weird reason, we have a special method to compare dates and that special method resides in EF dll. No clue why `Date.AddDays()` does not work – Garima Oct 12 '18 at 04:50

1 Answers1

1

we have a special method to compare dates and that special method resides in EF dll. No clue why Date.AddDays() does not work

No good reason. This was enabled in EF Core, which runs on .NET Framework as well as .NET Core. EG:

using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;


namespace EfCoreTest
{

    public class Article
    {
        [Key]
        public int ArticleId { get; set; }
        public string ArticleTitle { get; set; }
        public DateTime StartDate { get; set; }
        public int Period { get; set; }
    }
    public class Db : DbContext
    {
        readonly string connectionString;

        public Db(): this("server=.;database=EfCoreTest;Integrated Security=true")
        {

        }
        public Db(string connectionString)
        {
            this.connectionString = connectionString;
        }
        public DbSet<Article> Articles { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(connectionString);
            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }



    class Program
    {



        static void Main(string[] args)
        {


            using (var db = new Db())
            {
                db.Database.EnsureDeleted();
                db.Database.EnsureCreated();
                var r = db.Articles.Where(p => p.StartDate.AddDays(p.Period) < DateTime.Now).ToList();

            }

            Console.ReadKey();


        }
    }
}

translates to

SELECT [p].[ArticleId], [p].[ArticleTitle], [p].[Period], [p].[StartDate]
FROM [Articles] AS [p]
WHERE DATEADD(day, [p].[Period], [p].[StartDate]) < GETDATE()
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • Thanks actually I am already mid-way a project and cannot switch to EF Core, but for future I will keep this in mind. – Garima Oct 15 '18 at 06:36