Efdbcontext Cs
Efdbcontext Cs
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; }
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["db
"].ConnectionString);
}
modelBuilder.Entity<Customer>(c =>
{
c.Property(b => b.Address)
.HasColumnType("nvarchar(250)")
.IsRequired();
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();
}
}
}
}
}
}
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>