Entity Framework Core using SQL Server

This blog article illustrations how to use Entity Framework Core with SQL Server. The sample that you found here https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=visual-studio/?WT.mc_id=DP-MVP-36769 is using SqlLite.

To use SQL Server instead of

Install-Package Microsoft.EntityFrameworkCore.Sqlite

You need to install

Install-Package Microsoft.EntityFrameworkCore.sqlserver

Change the Model.cs accordingly.

public class BloggingContext : DbContext

{

    public DbSet<Blog> Blogs { get; set; }

    public DbSet<Post> Posts { get; set; }

    public string conn = “data source=.;initial catalog=blogging;integrated security=True”;

    protected override void OnConfiguring(DbContextOptionsBuilder options)

        => options.UseSqlServer(conn);

}

public class Blog

{

    public int BlogId { get; set; }

    public string Url { get; set; }

    public List<Post> Posts { get; } = new List<Post>();

}

public class Post

{

    public int PostId { get; set; }

    public string Title { get; set; }

    public string Content { get; set; }

    public int BlogId { get; set; }

    public Blog Blog { get; set; }

}

I altered a bit the program.cs as well as below.

static void Main()

{

    using (var db = new BloggingContext())

    {

        // Create

        Console.WriteLine(“Inserting a new blog”);

        db.Add(new Blog {BlogId=1, Url = http://blogs.msdn.com/adonet&#8221; });

        db.SaveChanges();

        // Read

        Console.WriteLine(“Querying for a blog”);

        var blog = db.Blogs

            .OrderBy(b => b.BlogId)

            .First();

        // Add Post

        Console.WriteLine(“Updating the blog and adding a post”);

        db.Add(new Post

        {

            PostId = 1,

            Title = “Hello World”,

            Content = “I wrote an app using EF Core!”,

            BlogId = 1

        }

            );

        db.SaveChanges();

        // Delete

        Console.WriteLine(“Delete the blog”);

        db.Remove(blog);

        db.SaveChanges();

    }

}

You should follow the rest of the step on the docs.microsoft link. Issue the comment in Visual Studio.

Install-Package Microsoft.EntityFrameworkCore.Tools

Add-Migration InitialCreate

Update-Database

And put the line in csproj file after the TargetFramework property,

<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>

You will see the below screen when you run the program.


Source code download: https://github.com/chanmmn/EFCoreSqlServer

Unknown's avatar

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet, Data Platform and tagged . Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.