v0.0.14 and examples

This commit is contained in:
28810 2019-02-16 17:38:54 +08:00
parent 2ca55c3d15
commit 5cfc359b80
24 changed files with 402 additions and 34 deletions

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace efcore_to_freesql.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

View File

@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
namespace efcore_to_freesql.DBContexts {
public class BaseDBContext : DbContext {
public static IFreeSql Fsql { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
Fsql.CodeFirst.ConfigEntity(modelBuilder.Model); //ͬ²½ÅäÖÃ
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseSqlite(@"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10");
}
}
}

View File

@ -0,0 +1,18 @@
using efcore_to_freesql.Entitys;
using Microsoft.EntityFrameworkCore;
namespace efcore_to_freesql.DBContexts {
public class Topic1Context : BaseDBContext {
public DbSet<Topic1> Topic1s { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Topic1>().ToTable("topic1_sss").HasKey(a => a.Id);
modelBuilder.Entity<Topic1>().Property(a => a.Id).HasColumnName("topic1_id").ValueGeneratedOnAdd();
base.OnModelCreating(modelBuilder);
}
}
}

View File

@ -0,0 +1,18 @@
using efcore_to_freesql.Entitys;
using Microsoft.EntityFrameworkCore;
namespace efcore_to_freesql.DBContexts {
public class Topic2Context : BaseDBContext {
public DbSet<Topic2> Topic2s { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Topic2>().ToTable("topic2_sss");
modelBuilder.Entity<Topic2>().Property(a => a.Id).HasColumnName("topic2_id");
base.OnModelCreating(modelBuilder);
}
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace efcore_to_freesql.Entitys {
public class Topic1
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace efcore_to_freesql.Entitys {
public class Topic2
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
}

View File

@ -0,0 +1,65 @@
using FreeSql;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public static class CodeFirstExtensions {
public static void ConfigEntity(this ICodeFirst codeFirst, IModel efmodel) {
foreach (var type in efmodel.GetEntityTypes()) {
codeFirst.ConfigEntity(type.ClrType, a => {
//表名
var relationalTableName = type.FindAnnotation("Relational:TableName");
if (relationalTableName != null)
a.Name(relationalTableName.Value?.ToString() ?? type.ClrType.Name);
foreach (var prop in type.GetProperties()) {
var freeProp = a.Property(prop.Name);
//列名
var relationalColumnName = prop.FindAnnotation("Relational:ColumnName");
if (relationalColumnName != null)
freeProp.Name(relationalColumnName.Value?.ToString() ?? prop.Name);
//主键
freeProp.IsPrimary(prop.IsPrimaryKey());
//自增
freeProp.IsIdentity(
prop.ValueGenerated == ValueGenerated.Never ||
prop.ValueGenerated == ValueGenerated.OnAdd ||
prop.GetAnnotations().Where(z =>
z.Name == "SqlServer:ValueGenerationStrategy" && z.Value.ToString().Contains("IdentityColumn") //sqlserver 自增
|| z.Value.ToString().Contains("IdentityColumn") //其他数据库实现未经测试
).Any()
);
//可空
freeProp.IsNullable(prop.AfterSaveBehavior != PropertySaveBehavior.Throw);
//类型
var relationalColumnType = prop.FindAnnotation("Relational:ColumnType");
if (relationalColumnType != null) {
var dbType = relationalColumnType.ToString();
if (!string.IsNullOrEmpty(dbType)) {
var maxLength = prop.FindAnnotation("MaxLength");
if (maxLength != null)
dbType += $"({maxLength})";
freeProp.DbType(dbType);
}
}
}
});
}
}
}

View File

@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace FreeSql.RESTful.Demo
namespace efcore_to_freesql
{
public class Program
{

View File

@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58143",
"sslPort": 44349
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"efcore_to_freesql": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using efcore_to_freesql.DBContexts;
using efcore_to_freesql.Entitys;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace efcore_to_freesql
{
public class Startup
{
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
{
Configuration = configuration;
Fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Pooling=true;Max Pool Size=10")
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
.UseAutoSyncStructure(true)
.Build();
DBContexts.BaseDBContext.Fsql = Fsql;
var sql11 = Fsql.Select<Topic1>().ToSql();
//SELECT a."Id", a."Title", a."CreateTime" FROM "Topic1" a
var sql12 = Fsql.Insert<Topic1>().AppendData(new Topic1()).ToSql();
//INSERT INTO "Topic1"("Id", "Title", "CreateTime") VALUES(@Id0, @Title0, @CreateTime0)
var sql21 = Fsql.Select<Topic2>().ToSql();
//SELECT a."Id", a."Title", a."CreateTime" FROM "Topic2" a
var sql22 = Fsql.Insert<Topic2>().AppendData(new Topic2()).ToSql();
//INSERT INTO "Topic2"("Id", "Title", "CreateTime") VALUES(@Id0, @Title0, @CreateTime0)
using (var db = new Topic1Context()) {
db.Topic1s.Add(new Topic1());
}
using (var db = new Topic2Context()) {
db.Topic2s.Add(new Topic2());
}
var sql13 = Fsql.Select<Topic1>().ToSql();
//SELECT a."topic1_id", a."Title", a."CreateTime" FROM "topic1_sss" a
var sql14 = Fsql.Insert<Topic1>().AppendData(new Topic1()).ToSql();
//INSERT INTO "topic1_sss"("Title", "CreateTime") VALUES(@Title0, @CreateTime0)
var sql23 = Fsql.Select<Topic2>().ToSql();
//SELECT a."topic2_id", a."Title", a."CreateTime" FROM "topic2_sss" a
var sql24 = Fsql.Insert<Topic2>().AppendData(new Topic2()).ToSql();
//INSERT INTO "topic2_sss"("Title", "CreateTime") VALUES(@Title0, @CreateTime0)
}
public IConfiguration Configuration { get; }
public IFreeSql Fsql { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IFreeSql>(Fsql);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
Console.InputEncoding = Encoding.GetEncoding("GB2312");
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
</ItemGroup>
</Project>

View File

@ -1,22 +1,10 @@
using System;
using Microsoft.AspNetCore.Mvc;
using restful.Entitys;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Npgsql.LegacyPostgis;
using FreeSql.RESTful.Demo.Entity;
namespace FreeSql.RESTful.Demo.Controllers {
namespace restful.Controllers {
[Route("restapi/[controller]")]

View File

@ -1,6 +1,6 @@
using FreeSql.DataAnnotations;
namespace FreeSql.RESTful.Demo.Entity {
namespace restful.Entitys {
public class Song {
[Column(IsIdentity = true)]

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace restful
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}

View File

@ -7,7 +7,7 @@ using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Text;
namespace FreeSql.RESTful.Demo {
namespace restful {
public class Startup
{
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -13,7 +13,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\FreeSql.Extensions.EFCoreModelBuilder\FreeSql.Extensions.EFCoreModelBuilder.csproj" />
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
</ItemGroup>

View File

@ -18,7 +18,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.Extensions.EFCoreMo
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{94C8A78D-AA15-47B2-A348-530CD86BFC1B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FreeSql.RESTful.Demo", "Examples\FreeSql.RESTful.Demo\FreeSql.RESTful.Demo.csproj", "{C32C7D4A-76D2-4A43-9A41-4B0440819954}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "restful", "Examples\restful\restful.csproj", "{83D10565-AF9D-4EDC-8FB8-8C962A843F97}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "efcore_to_freesql", "Examples\efcore_to_freesql\efcore_to_freesql.csproj", "{B93981B8-3295-4EDD-B314-BCA77B6BF37A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -78,24 +80,37 @@ Global
{490CC8AF-C47C-4139-AED7-4FB6502F622B}.Release|x64.Build.0 = Release|Any CPU
{490CC8AF-C47C-4139-AED7-4FB6502F622B}.Release|x86.ActiveCfg = Release|Any CPU
{490CC8AF-C47C-4139-AED7-4FB6502F622B}.Release|x86.Build.0 = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|x64.ActiveCfg = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|x64.Build.0 = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|x86.ActiveCfg = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Debug|x86.Build.0 = Debug|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|Any CPU.Build.0 = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|x64.ActiveCfg = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|x64.Build.0 = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|x86.ActiveCfg = Release|Any CPU
{C32C7D4A-76D2-4A43-9A41-4B0440819954}.Release|x86.Build.0 = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|x64.ActiveCfg = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|x64.Build.0 = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|x86.ActiveCfg = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Debug|x86.Build.0 = Debug|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|Any CPU.Build.0 = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|x64.ActiveCfg = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|x64.Build.0 = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|x86.ActiveCfg = Release|Any CPU
{83D10565-AF9D-4EDC-8FB8-8C962A843F97}.Release|x86.Build.0 = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|x64.ActiveCfg = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|x64.Build.0 = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|x86.ActiveCfg = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Debug|x86.Build.0 = Debug|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|Any CPU.Build.0 = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|x64.ActiveCfg = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|x64.Build.0 = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|x86.ActiveCfg = Release|Any CPU
{B93981B8-3295-4EDD-B314-BCA77B6BF37A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C32C7D4A-76D2-4A43-9A41-4B0440819954} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
{83D10565-AF9D-4EDC-8FB8-8C962A843F97} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
{B93981B8-3295-4EDD-B314-BCA77B6BF37A} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>0.0.13</Version>
<Version>0.0.14</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>YeXiangQin</Authors>
<Description>打造 .NETCore 最方便的 ORMDbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite 数据库。</Description>