0% found this document useful (0 votes)
10 views5 pages

Efdbcontext Cs

The document contains code for an Entity Framework DbContext class (EFDbContext) that manages a Customer entity, including its properties and configurations for a SQL Server database. It also includes a form (fManageCustomer) for managing customer data, allowing users to view and search for customers. Additionally, the App.config file defines the database connection string for the application.

Uploaded by

ANH nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Efdbcontext Cs

The document contains code for an Entity Framework DbContext class (EFDbContext) that manages a Customer entity, including its properties and configurations for a SQL Server database. It also includes a form (fManageCustomer) for managing customer data, allowing users to view and search for customers. Additionally, the App.config file defines the database connection string for the application.

Uploaded by

ANH nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

- EFDbContext.

cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLBH.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Configuration;

namespace QLBH
{
internal class EFDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }

public void ConfigureServices(IServiceCollection services)


{
services.AddDbContext<EFDbContext>();
}

protected override void OnConfiguring(DbContextOptionsBuilder


optionsBuilder)
{

optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["db
"].ConnectionString);
}

protected override void OnModelCreating(ModelBuilder


modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Customer>(c =>
{
c.Property(b => b.Address)
.HasColumnType("nvarchar(250)")
.IsRequired();

c.Property(b => b.BirthDay)


.HasColumnType("date");
});

modelBuilder.Entity<Customer>().HasData(
new Customer { CustomerID = 1, Name = "Lý Qua Cầu",
Gender = false, BirthDay = DateTime.Parse("1990-01-25"), Address =
"111/2 Lê Lợi, P5, Q1, TP. Hồ Chí Minh", Phone = "0123456789", Status
= true, Email = "[email protected]" },
new Customer { CustomerID = 2, Name = "Trần Văn Sơn",
Gender = true, BirthDay = DateTime.Parse("2000-11-20"), Address =
"2/10 Lý Thái Tổ, P1, Q2, TP. Hồ Chí Minh", Phone = "0923456789",
Status = true, Email = "[email protected]" },
new Customer { CustomerID = 3, Name = "Hoàng Dược Thảo
Mộc", Gender = null, BirthDay = DateTime.Parse("1980-05-15"), Address
= "9/2 Hoàng Diệu, P4, Q5, TP. Hồ Chí Minh", Phone = "0723456789",
Status = true, Email = "[email protected]" },
new Customer { CustomerID = 4, Name = "Cao Thu Trang",
Gender = false, BirthDay = DateTime.Parse("1991-07-09"), Address =
"666/3 Nguyễn Trãi, P1, Tân An, Long An", Phone = "0623456789", Status
= true, Email = "[email protected]" },
new Customer { CustomerID = 5, Name = "Nguyễn Kiểu",
Gender = false, BirthDay = DateTime.Parse("1982-03-04"), Address =
"466/3 Nguyễn Du, P6, Gò Dầu, Tây Ninh", Phone = "0823456789", Status
= true, Email = "[email protected]" });
}
}
}

- Customer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLBH.Models
{
internal class Customer
{
public long CustomerID { get; set; } //bigint,
identity(1,1), PK
[StringLength(100)]
public string Name { get; set; } //nvarchar(100), not
null
public bool? Gender { get; set; } //bit, null
[Column(TypeName = "Date")]
public DateTime BirthDay { get; set; }
[StringLength(250)]
public string Address { get; set; }
[StringLength(10, MinimumLength = 10), Column(TypeName =
"nchar(10)")]
public string Phone { get; set; }
[DataType(DataType.EmailAddress)]
[StringLength(100)]
public string? Email { get; set; }
public bool Status { get; set; } //bit, not null
}
}

- fManageCustomer.cs
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 QLBH
{
public partial class fManageCustomer : Form
{
public fManageCustomer()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object


sender, DataGridViewCellEventArgs e)
{

private void fManageCustomer_Activated(object sender,


EventArgs e)
{
{
using (var db = new EFDbContext())
{
dataGridView1.DataSource =
db.Customers.ToList();

}
}

private void btFind_Click(object sender, EventArgs e)


{
using (var db = new EFDbContext())
{
dataGridView1.DataSource = db.Customers
.Where(c =>
c.Name.Contains(txtName.Text)).ToList();
}
}

private void label2_Click(object sender, EventArgs e)


{

}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="db" connectionString="Server=DESKTOP-7VHNCSQ\
SQLEXPRESS;
Database=QLBH;Trusted_Connection=True;TrustServerCertificate=True;" />
</connectionStrings>
</configuration>

You might also like