CodeFirst approach allows creating entities in DB from code…
lets start new console app and create User class
1 2 3 4 5 6 |
class User { public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } } |
Now SolutionExplorer > Manage NuGetPackages for Solution > type Entity in search box and Install it to your project
Add using System.Data.Entity if not added before…
Now lets create UserContextClass like this
1 2 3 4 5 |
class UserContext: DbContext { public UserContext() : base("DefaultConnection") { } public DbSet<User> Users { get; set; } } |
Now go to SolutionExplorer and add ConnectionString
1 2 3 4 |
<connectionStrings> <add name ="DBConnection" connectionString="Data Source=DESKTOP-T9U6IEP\SQLEXPRESS;Initial Catalog=userstore.mdf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; " providerName="System.Data.SqlClient" /> </connectionStrings> |
How to get connection string?
Tools > SQLServerObjectExplorer > Add SQL Server if needed > RightClick on Server and Choose Properties > Connection string will be the first in opened Panel
it will be something like this
1 |
Data Source=DESKTOP-T9U6IEP\SQLEXPRESS;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False |
you need to add
1 |
providerName="System.Data.SqlClient |
then it will be
1 |
connectionString="Data Source=DESKTOP-T9U6IEP\SQLEXPRESS;Initial Catalog=userstore.mdf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; " providerName="System.Data.SqlClient" |
so in App.config should be something like this
1 2 3 4 |
<connectionStrings> <add name ="DBConnection" connectionString="Data Source=DESKTOP-T9U6IEP\SQLEXPRESS;Initial Catalog=userstore.mdf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; " providerName="System.Data.SqlClient" /> </connectionStrings> |
this also be Ok, changed Data Source=DESKTOP-T9U6IEP to (local)
1 2 3 |
<connectionStrings> <add name ="DBConnection" connectionString="(local)\SQLEXPRESS;Initial Catalog=userstore.mdf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; " providerName="System.Data.SqlClient" /> </connectionStrings> |
and this. changed (local) to .
1 2 3 |
<connectionStrings> <add name ="DBConnection" connectionString=".\SQLEXPRESS;Initial Catalog=userstore.mdf;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False; " providerName="System.Data.SqlClient" /> </connectionStrings> |
Now go to main and type like this – this code will create DB if needed and will add tables and fill records, and remove records if needed – see example…
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 |
class Program { static void Main(string[] args) { using (UserContext db = new UserContext()) { // writing new objects to dataBase User user1 = new User { Name = "Tom", Age = "23" }; User user2 = new User { Name = "Stas", Age = "24" }; db.Users.Add(user1); // adding db.Users.Add(user2); db.Users.Remove(user2); // removing db.SaveChanges(); Console.WriteLine("Objects successfully added"); // reading objects from dataBase var users = db.Users; Console.WriteLine("objects list"); foreach(User u in users) { Console.WriteLine("{0}.{1} - {2}", u.Id,u.Name,u.Age); } } Console.Read(); } } |