Thanks to visit codestin.com
Credit goes to github.com

Skip to content

vxchin/odata-efcore-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

odata-efcore-integration

Use DbContext.Model to configure the edm model.

使用 DbContext 上配置的模型(Microsoft.EntityFrameworkCore.Metadata.IModel 的示例)自动配置 OData 模型中的复杂类型和实体类型。

Usage

  • Step 1: Configure your Entity Framework Core Model with OnModelCreating.
public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    
    // ...
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // ...
    }
}
  • Step 2: Register your DbContext and then use EdmModel.CreateConventionFactory() static method to create a IEdmModel factory.
public void ConfigureServices(IServiceCollection services)
{
    void UseSqlServer(DbContextOptionsBuilder optionsBuilder) =>
        optionsBuilder.UseSqlServer(Configuration.GetConnectionString("ApplicationDb"));
        
    services.AddDbContext<ApplicationDbContext>(UseSqlServer);

    // ...
}    
  • Step 3: Use EdmModel.CreateConventionFactory() static method to create a IEdmModel factory, and then call ConfigureWithDbContext or ConfigureWithEntityFrameeworkModel.
public void ConfigureServices(IServiceCollection services)
{
    // ...
    
    services.AddController().AddOData(options => options
        .AddRouteComponents("odata", CreateEdmModel(UseSqlServer)));
}

private static IEdmModel CreateEdmModel(Action<DbContextOptionsBuilder> optionsAction)
{
    return EdmModel.CreateConventionFactory()
        .ConfigureWithDbContext<ApplicationDbContext>(optionsAction)
        .Configure(builder =>
        {
            // Additional configuration for the edm model.
        }).CreateEdmModel();
}

About

Use DbContext.Model to configure the edm model.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages