✨ feat(新增): EfCore基础配置
This commit is contained in:
parent
68d951b8f3
commit
f0b1ecde21
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="bunny" uuid="1a151f4f-7b33-40a5-b425-02b84edb60e6">
|
||||||
|
<driver-ref>sqlite.xerial</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:sqlite:D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\bunny.db</jdbc-url>
|
||||||
|
<jdbc-additional-properties>
|
||||||
|
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||||
|
</jdbc-additional-properties>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
</data-source>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,26 @@
|
||||||
|
using Bunny.Dao.Entity.Blog;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Bunny.Common.EFCore;
|
||||||
|
|
||||||
|
public class EfCoreContext : DbContext
|
||||||
|
{
|
||||||
|
public EfCoreContext()
|
||||||
|
{
|
||||||
|
// var dbPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "bunny.db");
|
||||||
|
// DbPath = Path.Join(dbPath, "bunny.db");
|
||||||
|
// DbPath = @"D:\Project\web\Bunny-Cli\template\CSharp\CSharp-Single-EFCore\Bunny.WebApi\bunny.db";
|
||||||
|
var dataBaseConnection = AppSettings.GetAppConfig<string>("DataBase:DataBaseConnection");
|
||||||
|
DbPath = dataBaseConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<Blog> Blogs { get; set; }
|
||||||
|
public DbSet<Post> Posts { get; set; }
|
||||||
|
|
||||||
|
public string DbPath { get; }
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||||
|
{
|
||||||
|
options.UseSqlite($"Data Source={DbPath}");
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,15 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Bunny.Dao.Entity.Blog;
|
||||||
|
|
||||||
|
public class Blog
|
||||||
|
{
|
||||||
|
public int BlogId { get; set; }
|
||||||
|
public string Url { get; set; }
|
||||||
|
|
||||||
|
public List<Post> Posts { get; } = [];
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
namespace Bunny.Dao.Entity.Blog;
|
||||||
|
|
||||||
|
public class Post
|
||||||
|
{
|
||||||
|
public int PostId { get; set; }
|
||||||
|
public string? Title { get; set; }
|
||||||
|
public string? Content { get; set; }
|
||||||
|
|
||||||
|
public int BlogId { get; set; }
|
||||||
|
public Blog? Blog { get; set; }
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
using Bunny.Common.EFCore;
|
||||||
|
using Bunny.Dao.Entity.Blog;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Bunny.Test.Until.EfCoreTest;
|
||||||
|
|
||||||
|
public class EfCoreTest
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void TestDbConnection()
|
||||||
|
{
|
||||||
|
using var db = new EfCoreContext();
|
||||||
|
Console.WriteLine($"Database path: {db.DbPath}.");
|
||||||
|
|
||||||
|
// Create
|
||||||
|
Console.WriteLine("Inserting a new blog");
|
||||||
|
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
|
||||||
|
db.SaveChanges();
|
||||||
|
|
||||||
|
// Read
|
||||||
|
Console.WriteLine("Querying for a blog");
|
||||||
|
var blog = db.Blogs
|
||||||
|
.OrderBy(b => b.BlogId)
|
||||||
|
.First();
|
||||||
|
Console.WriteLine(blog.Url);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
<TestId>NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.GetProperties</TestId>
|
<TestId>NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.NetUtilTest.GetProperties</TestId>
|
||||||
<TestId>NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Redis.RedisConnect.RedisConnectTest</TestId>
|
<TestId>NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Redis.RedisConnect.RedisConnectTest</TestId>
|
||||||
<TestId>xUnit::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Redis.RedisConnect.RedisConnectTest</TestId>
|
<TestId>xUnit::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.Redis.RedisConnect.RedisConnectTest</TestId>
|
||||||
|
<TestId>NUnit3x::945BE294-8057-40EE-9A98-5598D1A728B9::net8.0::Bunny.Test.Until.EfCoreTest.EfCoreTest.TestDbConnection</TestId>
|
||||||
</TestAncestor>
|
</TestAncestor>
|
||||||
</SessionState></s:String>
|
</SessionState></s:String>
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
using Bunny.Common.Exception;
|
using Bunny.Common.EFCore;
|
||||||
|
using Bunny.Common.Exception;
|
||||||
|
using Bunny.Dao.Entity.Blog;
|
||||||
using Bunny.Dao.Models.System;
|
using Bunny.Dao.Models.System;
|
||||||
using Bunny.Dao.Result;
|
using Bunny.Dao.Result;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -8,7 +8,7 @@
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"DataBase": {
|
"DataBase": {
|
||||||
"SqlServerConnection": "Data Source=192.168.3.98;Initial Catalog=BunnyTest;Persist Security Info=True;User ID=sa;Password=abc1234.",
|
"DataBaseConnection": "Database/bunny.db",
|
||||||
"TimeOut": 6
|
"TimeOut": 6
|
||||||
},
|
},
|
||||||
"JWT": {
|
"JWT": {
|
||||||
|
|
Loading…
Reference in New Issue