Exemplo n.º 1
0
        private static void SetupDatabase()
        {
            using (var db = new BloggingContext())
            {
                if (db.Database.EnsureCreated())
                {
                    db.Database.ExecuteSqlCommand("CREATE FUNCTION [dbo].[SearchBlogs] (@term nvarchar(200)) RETURNS TABLE AS RETURN (SELECT * FROM dbo.Blogs WHERE Url LIKE '%' + @term + '%')");

                    db.Blogs.Add(new Blog { Url = "http://sample.com/blogs/fish" });
                    db.Blogs.Add(new Blog { Url = "http://sample.com/blogs/catfish" });
                    db.Blogs.Add(new Blog { Url = "http://sample.com/blogs/cats" });
                    db.SaveChanges();
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            SetupDatabase();

            using (var db = new BloggingContext())
            {
                Console.Write("Enter a search term: ");
                var term = Console.ReadLine();

                var blogs = db.Blogs.FromSql("SELECT * FROM dbo.SearchBlogs(@p0)", term)
                    .OrderBy(b => b.Url)
                    .Select(b => b.Url);

                foreach (var blog in blogs)
                {
                    Console.WriteLine(blog);
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            SetupDatabase();

            using (var db = new BloggingContext())
            {
                Console.Write("Enter a search term: ");
                var term = Console.ReadLine();

                // TODO Use TVF for search
                var blogs = db.Blogs
                    .Where(b => b.Url.Contains(term))
                    .ToList();

                foreach (var blog in blogs)
                {
                    Console.WriteLine(blog.Url);
                }
            }
        }