Today i am in mode to write ef 7 with code first with .NET core application.
1.First install .net framework 4.6.2 or latest from Microsoft official site .
2.Check install by writing dotnet in cmd.
it will show
Microsoft .NET Core Shared Framework HostVersion : 1.0.1
Build : cee57bf6c981237d80aa1631cfe83cb9ba329f12
.....
3.create a folder dotnetCore and write cd dotnetCore in cmd and then
i.dotnet new
ii.dotnet restore
iii.dotnet run
you will see Hello World!
in console.
There you go ..You have created a first project in dot net core with cmd .Will drive into Sql Server with EF 7 code first now.
4.You have see two file i.Program.cs and ii.project.json in your directory.
the file project.json with contain all my dependencies .
Write your json file like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final" }, "dependencies": { "NETStandard.Library": "1.6.0", "Microsoft.EntityFrameworkCore": "1.0.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0", "Microsoft.EntityFrameworkCore.Design": { "version": "1.0.0-preview2-final", "type": "build" }, "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" } }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "imports": "dnxcore50" } } } |
and Program.cs file like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using System.Linq; namespace ConsoleApplication { public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=.;Database=log;Persist Security Info=True;User ID=sa;Password=pass"); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } //public string BlogName { get; set; } public List<Post> Posts { get; set; } } 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; } } public class Program { public static void Main(string[] args) { try{ using (var db = new BloggingContext()) { var blog = new Blog { Url = "http://sample.com" }; db.Blogs.Add(blog); db.SaveChanges(); } } catch(Exception e){ Console.WriteLine(e.InnerException.ToString()); } Console.WriteLine("Hello World!..Database Added successfully"); } } } |
if every thing is all right ..
Then wirte this three line of code
dotnet restore
dotnet ef migrations add MyMigration
dotnet ef database update
dotnet run.
It will insert database with table Blogs.
5.If you want to add a column name
public string BlogName { get; set; }
then run this command
dotnet ef migrations add MyAddedNewMigration
dotnet ef database update
you will see new column is added to database in table Blogs