提交 c8dc81bf 编写于 作者: S simon

EFcore 访问数据库成功

上级 07ee39d6
using System;
using System.Collections.Generic;
using System.Text;
namespace SmartDw.DemocracyOrg.Domain
{
public interface IBaseEntity<TKey, TKey2> : IEntity<TKey, TKey2>
{
/// <summary>
/// row创建时间
/// </summary>
DateTimeOffset? RowCreated { get; set; }
/// <summary>
/// row创建人
/// </summary>
string RowCreatedBy { get; set; }
/// <summary>
/// row更新时间
/// </summary>
DateTimeOffset? RowUpdated { get; set; }
/// <summary>
/// row更新人
/// </summary>
string RowUpdatedBy { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Aurora.Common.Entities
{
public interface IEntity
{
/// <summary>
/// 租户Code
/// </summary>
string TenantCode { get; set; }
/// <summary>
/// 主键
/// </summary>
int ID { get; set; }
DateTimeOffset CreateTime { get; set; }
string CreateUserName { get; set; }
DateTimeOffset? ModifyTime { get; set; }
string ModifyUserName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace SmartDw.DemocracyOrg.Domain
{
public interface IEntity
{
}
public interface IEntity<TKey, TKey2> : IEntity
{
/// <summary>
/// 租户Code
/// </summary>
TKey TenantCode { get; set; }
/// <summary>
/// 主键
/// </summary>
TKey2 ID { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Aurora.Common.Entities
{
public interface ITenantEntity:IEntity
{
/// <summary>
/// 租户Code
/// </summary>
string TenantCode { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Aurora.Common.Entities
{
public interface IEntity
{
/// <summary>
/// 租户Code
/// </summary>
string TenantCode { get; set; }
/// <summary>
/// 主键
/// </summary>
int ID { get; set; }
DateTimeOffset CreateTime { get; set; }
string CreateUserName { get; set; }
DateTimeOffset? ModifyTime { get; set; }
string ModifyUserName { get; set; }
}
}

using Aurora.Common.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
namespace Aurora.Core.EntityFramework.EntitiesConfig
{
/// <summary>
/// 对父类领域实体配置
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseEntityTypeConfiguration<T> : IEntityTypeConfiguration<T>
where T : class, ITenantEntity
{
public void Configure(EntityTypeBuilder<T> builder)
{
builder.Property(x => x.TenantCode).HasMaxLength(100);
builder.HasKey(x => new { x.TenantCode, x.ID });
// builder.HasIndex(x => new { x.TenantCode, x.ID });
// builder.Property(x => x.RowCreatedBy).HasMaxLength(100);
// builder.Property(x => x.RowUpdatedBy).HasMaxLength(100);
Map(builder);
}
/// <summary>
/// 强行让子类实现自己配置
/// </summary>
/// <param name="builder"></param>
public abstract void Map(EntityTypeBuilder<T> builder);
}
}
using System;
namespace Aurora.Core.EntityFramework.EntitiesConfig
{
public class EmptyClass
{
public EmptyClass()
{
}
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Aurora.Storage.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Aurora.Storage
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:24743",
"sslPort": 44318
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Aurora.Storage": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
namespace Aurora.Storage
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Aurora.Storage", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Aurora.Storage v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
using System;
namespace Aurora.Storage
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册