添加EFCoreTest项目

Former-commit-id: 1c394a4d4b42a2f7aac54cd74d87840e2b913b5b
TangShanKaiPing
wanggang 5 years ago
parent 84ec6a30a9
commit 9bd852f7b4

Binary file not shown.

@ -0,0 +1,18 @@
*.bak
*.suo
*.db
*.db-shm
*.db-wal
*.user
.vs
obj
Obj
bin
Bin
debug
Debug
release
Release
Logs
logs
node_modules

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
</ItemGroup>
</Project>

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29814.53
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreTest", "EFCoreTest.csproj", "{4BB784D0-B6BB-4450-9E34-F64EA74B5555}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BB784D0-B6BB-4450-9E34-F64EA74B5555}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C2F655E3-914B-48C5-A6C5-7674A05F5CFD}
EndGlobalSection
EndGlobal

@ -0,0 +1,71 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EFCoreTest
{
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=data.db;");
//optionsBuilder.UseMySql("Server=139.180.143.95;Port=3306;Database=test;Uid=admin;Pwd=admin;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>();
modelBuilder.Entity<Role>();
modelBuilder.Entity<UserRole>().HasOne(o => o.User).WithMany(o => o.UserRoles).HasForeignKey(o => o.UserId);
modelBuilder.Entity<UserRole>().HasOne(o => o.Role).WithMany(o => o.UserRoles).HasForeignKey(o => o.RoleId);
modelBuilder.Entity<UserRole>().HasIndex(o => new { o.UserId, o.RoleId }).IsUnique();
}
}
public class Entity
{
public Guid Id { get; set; }
public Entity()
{
this.Id = Guid.NewGuid();
}
}
public class User : Entity
{
public string UserName { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class Role : Entity
{
public string Name { get; set; }
public List<UserRole> UserRoles { get; set; } = new List<UserRole>();
}
public class UserRole : Entity
{
public Guid UserId { get; set; }
public Guid RoleId { get; set; }
public User User { get; set; }
public Role Role { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var db = new MyDbContext())
{
if (db.Database.EnsureCreated())
{
db.Set<User>().Add(new User { UserName = "test" });
db.SaveChanges();
}
Console.WriteLine(db.Set<User>().FirstOrDefault()?.UserName);
}
}
}
}
Loading…
Cancel
Save