Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views3 pages

Database Setup

The document provides code snippets for establishing database connections using Java and .NET, including connection strings and configurations for SQL Server and MySQL. It also outlines commands for scaffolding models and managing migrations in Entity Framework, along with examples of seeding data and defining entity properties. Additionally, it lists necessary packages and tools required for working with Entity Framework in .NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Database Setup

The document provides code snippets for establishing database connections using Java and .NET, including connection strings and configurations for SQL Server and MySQL. It also outlines commands for scaffolding models and managing migrations in Entity Framework, along with examples of seeding data and defining entity properties. Additionally, it lists necessary packages and tools required for working with Entity Framework in .NET.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

JAVA:

Connection connection = null;


String userName = "sa";
String passWord = "123";
String port = "1433";
String ip = "127.0.0.1";
String dbName = "***********";
String deviceName = "LAPTOP-5D2CNVK4";
String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String sqlUrl = "jdbc:sqlserver://LAPTOP-
5D2CNVK4;databaseName=*********;encrypt=false;trustServerCertificate=false;loginTim
eout=30";
try {
Class.forName(driverClass);
connection = DriverManager.getConnection(sqlUrl, userName, passWord);
} catch (SQLException s) {
System.out.println("Connect Failed");
}
return connection;

DOTNET:

JSON CONNECTION STRING:


{
"ConnectionStrings": {
"MyStockDB":
"Server=(local);uid=sa;pwd=123;database=*************;encrypt=false;Trusted_Connect
ion=True;trustServerCertificate=true"
}
}

GET CONNECTION STRING:


protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var builder = new
ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("ap
psettings.json", optional: true, reloadOnChange: true);
Console.Write(builder.ToString());
IConfigurationRoot configuration = builder.Build();
Cach1 :
optionsBuilder.UseSqlServer("Server=(local);uid=sa;pwd=123;database=MyStockDB;trust
ServerCertificate=true;");
Cach2 :
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DATABASE_NAME"));
}

DATABASE FIRST:
TERMINAL COMMAND TO GET MODEL:
dotnet ef dbcontext scaffold Name=ConnectionStrings:NameDeclaredInJson
Microsoft.EntityFrameworkCore.SqlServer --output-dir ./ (Have info in Json)
OR
(MYSQL)
dotnet ef dbcontext scaffold
"server=localhost;port=3306;user=root;password=admin@123;database=(Tên DB trong
sql)" Pomelo.EntityFrameworkCore.MySql --output-dir Models(Thư mục chứa entity) --
context-dir Data(Thư mục chứa dbcontext) --context PRNDbContext(Tên context) --use-
database-names --no-onconfiguring --force (--no-build nếu build bị lỗi)
(MSSQL)
dotnet ef dbcontext scaffold "server=(local);database=(Tên DB trong
sql);uid=sa;pwd=123;encrypt=false;Trusted_Connection=True;trustServerCertificate=tr
ue" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models(Thư mục chứa
entity) --context-dir Data(Thư mục chứa dbcontext) --context PRNDbContext(Tên
context) --use-database-names --no-onconfiguring --force (--no-build nếu build bị
lỗi)

CODE FIRST:
TERMINAL CODE: dotnet ef migrations Add "Custom name history" ----> dotnet ef
database update
CODE TO GENERATE MODEL:

protected override void OnModelCreating(ModelBuilder modelBuilder)


{
modelBuilder.Entity<Category>().Property(category =>
category.CategoryName).IsRequired().HasMaxLength(49);

modelBuilder.Entity<Category>().HasData(
new Category { CategoryID = 1, CategoryName = "Beverages" },
new Category { CategoryID = 2, CategoryName = "Condiments" },
new Category { CategoryID = 3, CategoryName = "Confections" });
}

SEEDING DATA:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AccountMember>().HasData(
new AccountMember { MemberId = "PS0001", MemberPassword = "@1",
EmailAddress = "PS0001", MemberRole = 1, FullName = "Adminstrator" },
new AccountMember { MemberId = "2", MemberPassword = "123",
EmailAddress = "[email protected]", MemberRole = 0, FullName = "Nguyen Tuan" }
);
modelBuilder.Entity<Category>().Property(c =>
c.CategoryId).ValueGeneratedOnAdd().UseIdentityColumn();
modelBuilder.Entity<Product>().HasOne(c => c.Category).WithMany(p =>
p.Products).HasForeignKey(c => c.CategoryId);
modelBuilder.Entity<Category>().HasData(
new Category { CategoryId = 1, CategoryName = "Beverages" },
new Category { CategoryId = 2, CategoryName = "Condiments" },
new Category { CategoryId = 3, CategoryName = "Confections" },
new Category { CategoryId = 4, CategoryName = "Dairy Products" },
new Category { CategoryId = 5, CategoryName = "Grains/Cereals" },
new Category { CategoryId = 6, CategoryName = "Meat/Poultry" },
new Category { CategoryId = 7, CategoryName = "Produce" },
new Category { CategoryId = 8, CategoryName = "Seafood" }
);

modelBuilder.Entity<Product>().Property(p =>
p.ProductId).ValueGeneratedOnAdd().UseIdentityColumn();
}
}

PACKAGE:
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer || Pomelo.EntityFrameworkCore.MySql
Microsoft.EntityFrameworkCore.Tools
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
dotnet tool install --global dotnet-ef

You might also like