项目作者: holydk

项目描述 :
A entity to entity configuration mapping for Entity Framework Core.
高级语言: C#
项目地址: git://github.com/holydk/AutoContext.EntityFrameworkCore.git
创建时间: 2019-09-05T14:29:16Z
项目社区:https://github.com/holydk/AutoContext.EntityFrameworkCore

开源协议:MIT License

下载


AutoContext

AutoContext.EntityFrameworkCore is a lightweight extension of Entity Framework Core for entity to entity configuration mapping.

How to build

AutoContext.EntityFrameworkCore is built against the latest ASP.NET Core 3.

  • Install the latest .NET Core 3.x SDK
  • Run build.ps1 in the root of the repo

Getting Started

  • Install nuget package to a new or existing project
    1. Install-Package AutoContext.EntityFrameworkCore
  • Define your entities
    1. public class YourEntity
    2. {
    3. public int Id { get; set; }
    4. }
  • Define your context
    ```csharp
    public class YourContext : AutoContextBase
    {
    public YourContext(DbContextOptions options)
    1. : base(options)
    {
    }
    }

// or for context without inheritance from AutoContext
public class YourContextWithoutInheritanceFromAutoContext : DbContext
{
public YourContextWithoutInheritance(DbContextOptions options)
: base(options)
{
}

  1. protected override void OnModelCreating(ModelBuilder modelBuilder)
  2. {
  3. Database.ApplyConfigurations(modelBuilder);
  4. base.OnModelCreating(modelBuilder);
  5. }

}

  1. * Define your mapping configurations in any assembly
  2. ```csharp
  3. [MappingConfiguration(contextType: typeof(YourContext))]
  4. public abstract class YourBaseMapping<TEntity> : AbstractEntityTypeConfiguration<TEntity>
  5. where TEntity : class
  6. {
  7. }
  8. public class YourCustomEntityMapping : YourBaseMapping<YourEntity>
  9. {
  10. public virtual void Configure(EntityTypeBuilder<TEntity> builder)
  11. {
  12. // Your configuration here
  13. }
  14. }
  • Register your context

    1. services.AddDbContext<YourContext>(options =>
    2. {
    3. // other registrations
    4. // If the mapping assembly name is null then will be used assembly of YourContext.
    5. options.UseMappingAssembly();
    6. // or
    7. options.UseMappingAssembly(<yourMappingAssemblyName>);
    8. });