Windows Form Application with Entity Freamwork
Windows Form Application with Entity Framework, SQL Server
Open a Windows form Application.
Firstly Create a Model
Right Click the Solution >> Add >> Select the class >> type the appropriate Name for the model.
Then Go to Menu Select Project >> Manage Nuget Packages >> Online >> Select Entity Framework and Install.
This my UI interface
Two Tables have for this project
Here is my Model.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_WindowsFormsApplication1
{
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public virtual List<paddress> paddresss { get; set; }
}
public class paddress
{
public int Paddressid { get; set; }
public string Address { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
}
Should create a separate class for data database access.
Right-click the solution. Add a folder name it as DAL.
Inside the DAL Folder create a class name it as PersonDbContext
this is the Context class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace EF_WindowsFormsApplication1
{
class PersonDbContext : DbContext
{
public PersonDbContext()
: base("name=PersonContext")
{
}
public DbSet<Person> Persons { get; set; }
public DbSet<paddress> paddress { get; set; }
}
}
Following part should add after the entity-framework tag to App.config file
<connectionStrings>
<add name="PersonContext"
connectionString="Data Source=.;Initial Catalog=mahiDb;
Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Code file like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EF_WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Here First Name should be Capital
if (!Regex.Match(tb_Fname.Text, "^[A-Z][a-zA-Z]*$").Success)
{
// first name was incorrect
MessageBox.Show("Invalid first name", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
tb_Fname.Focus();
return;
}
using (var db = new PersonDbContext())
{
var person = new Person
{
FirstName = tb_Fname.Text,
LastName = tb_Lname.Text,
BirthDate = DateTime.Now,
};
db.Persons.Add(person);
//db.SaveChanges();
var padd = new paddress
{
Address = tb_address.Text,
};
db.paddress.Add(padd);
db.SaveChanges();
}
MessageBox.Show("Person saved !");
}
}
}
Open a Windows form Application.
Firstly Create a Model
Right Click the Solution >> Add >> Select the class >> type the appropriate Name for the model.
Then Go to Menu Select Project >> Manage Nuget Packages >> Online >> Select Entity Framework and Install.
This my UI interface
Two Tables have for this project
Here is my Model.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_WindowsFormsApplication1
{
public class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public virtual List<paddress> paddresss { get; set; }
}
public class paddress
{
public int Paddressid { get; set; }
public string Address { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
}
Should create a separate class for data database access.
Right-click the solution. Add a folder name it as DAL.
Inside the DAL Folder create a class name it as PersonDbContext
this is the Context class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace EF_WindowsFormsApplication1
{
class PersonDbContext : DbContext
{
public PersonDbContext()
: base("name=PersonContext")
{
}
public DbSet<Person> Persons { get; set; }
public DbSet<paddress> paddress { get; set; }
}
}
Following part should add after the entity-framework tag to App.config file
<connectionStrings>
<add name="PersonContext"
connectionString="Data Source=.;Initial Catalog=mahiDb;
Integrated Security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Code file like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace EF_WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Here First Name should be Capital
if (!Regex.Match(tb_Fname.Text, "^[A-Z][a-zA-Z]*$").Success)
{
// first name was incorrect
MessageBox.Show("Invalid first name", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
tb_Fname.Focus();
return;
}
using (var db = new PersonDbContext())
{
var person = new Person
{
FirstName = tb_Fname.Text,
LastName = tb_Lname.Text,
BirthDate = DateTime.Now,
};
db.Persons.Add(person);
//db.SaveChanges();
var padd = new paddress
{
Address = tb_address.Text,
};
db.paddress.Add(padd);
db.SaveChanges();
}
MessageBox.Show("Person saved !");
}
}
}
Comments
Post a Comment