本文整理汇总了C#中IServiceCollection.AddEntityFramework方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceCollection.AddEntityFramework方法的具体用法?C# IServiceCollection.AddEntityFramework怎么用?C# IServiceCollection.AddEntityFramework使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceCollection
的用法示例。
在下文中一共展示了IServiceCollection.AddEntityFramework方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<DomainModelSqliteContext>();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<DomainModelMsSqlServerContext>();
JsonOutputFormatter jsonOutputFormatter = new JsonOutputFormatter
{
SerializerSettings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
};
services.AddMvc(
options =>
{
options.OutputFormatters.Clear();
options.OutputFormatters.Insert(0, jsonOutputFormatter);
}
);
// Use a SQLite database
services.AddScoped<IDataAccessProvider, DataAccessSqliteProvider>();
// Use a MS SQL Server database
//services.AddScoped<IDataAccessProvider, DataAccessMsSqlServerProvider>();
}
示例2: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
//Swagger additons
services.AddMvcCore()
.AddApiExplorer();
services.AddSwaggerGen();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<BookServiceContext>(options =>
options.UseSqlServer(Configuration["ConnectionString"]));
//options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=BookServiceContext-70e8c6ed-94f8-4d84-97df-d0729ea62482;Trusted_Connection=True;MultipleActiveResultSets=true"));
}
示例3: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<ProjectDbContext>(options =>
{
options.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"]);
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
示例4: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<OrdersContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
示例5: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:TJCMVCOnlyContext:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<TJCMVCOnlyContext>(options =>
options.UseSqlServer(Configuration["Data:TJCMVCOnlyContext:ConnectionString"]));
}
示例6: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
var connection = @"Server=DESKTOP-2N2TC81;Database=NeedleWork2016;Trusted_Connection=True;";
//var connection = @"Data Source=10.10.200.62;Initial Catalog=NeedleWork2016;Persist Security Info=True;User ID=needlework2016;Password=Qwerty1!";
services.AddEntityFramework()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connection));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddMvc();
services.AddMvc(options =>
{
options.Filters.Add(new ConvertToPdfFilterAttribute());
options.Filters.Add(new CultureAttribute());
});
}
示例7: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
//Sql client not available on mono
var useInMemoryStore = Configuration["UseInMemoryStore"] == "true" ?
true :
_runtimeEnvironment.RuntimeType.Equals("Mono", StringComparison.OrdinalIgnoreCase);
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/Home/AccessDenied");
})
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
//Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCaching();
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build());
});
}
示例8: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<PeopleContext>(options =>
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=PeopleContext-584a36ef-5591-4b99-9bc0-09bdade67193;Trusted_Connection=True;MultipleActiveResultSets=true"));
services.AddTransient<IPeopleService, PeopleService>();
}
示例9: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
if (Configuration["Data:UseSqLite"] == "true")
{
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration["Data:DefaultConnection:SqLiteConnectionString"]));
}
else
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:MsSqlConnectionString"]));
}
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
//Password policy
//stackoverflow.com/questions/27831597/how-do-i-define-the-password-rules-for-identity-in-asp-net-5-mvc-6-vnext
services.AddIdentity<ApplicationUser, IdentityRole>(o => {
// configure identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonLetterOrDigit = false; ;
o.Password.RequiredLength = 6;
}).AddDefaultTokenProviders();
}
示例10: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
//Sql client not available on mono
var usingMono = Type.GetType("Mono.Runtime") != null;
// Add EF services to the services container
if (usingMono)
{
services.AddEntityFramework(Configuration)
.AddInMemoryStore()
.AddDbContext<MoviesAppContext>();
} else {
services.AddEntityFramework(Configuration)
.AddSqlServer()
.AddDbContext<MoviesAppContext>(options =>
{
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
});
}
// add ASP.NET Identity
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
.AddEntityFrameworkStores<MoviesAppContext>();
// add ASP.NET MVC
services.AddMvc();
}
示例11: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
var useInMemoryStore = _platform.IsRunningOnMono || _platform.IsRunningOnNanoServer;
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options =>
options.UseInMemoryDatabase());
}
else
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http://example.com");
});
});
// Add MVC services to the services container
services.AddMvc();
// Add memory cache services
services.AddCaching();
// Add session related services.
services.AddSession();
// Add the system clock service
services.AddSingleton<ISystemClock, SystemClock>();
// Configure Auth
services.AddAuthorization(options =>
{
options.AddPolicy(
"ManageStore",
authBuilder => {
authBuilder.RequireClaim("ManageStore", "Allowed");
});
});
}
示例12: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
//Sql client not available on mono
var useInMemoryStore = Type.GetType("Mono.Runtime") != null;
// Add EF services to the services container
if (useInMemoryStore)
{
services.AddEntityFramework()
.AddInMemoryStore()
.AddDbContext<MusicStoreContext>();
}
else
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MusicStoreContext>(options =>
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")));
}
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
services.ConfigureOpenIdConnectAuthentication(options =>
{
options.Authority = "https://login.windows.net/[tenantName].onmicrosoft.com";
options.ClientId = "[ClientId]";
});
// Add MVC services to the services container
services.AddMvc();
//Add all SignalR related services to IoC.
services.AddSignalR();
//Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCaching();
services.AddSession();
// Configure Auth
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build());
});
}
示例13: ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
var _serv = services.BuildServiceProvider();
var appRoot = _serv.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
IConfiguration Configuration;
services.AddConfiguration(out Configuration);
if (Configuration["Data:DefaultConnection:Mode"] == "SQLite")
{
services.AddEntityFramework()
.AddDbContext<vNextChinaContext>(x => x.UseSqlite(Configuration["Data:DefaultConnection:ConnectionString"].Replace("{appRoot}", appRoot)))
.AddSqlite();
}
else if (Configuration["Data:DefaultConnection:Mode"] == "SqlServer")
{
services.AddEntityFramework()
.AddDbContext<vNextChinaContext>(x => x.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
.AddSqlServer();
}
else
{
services.AddEntityFramework()
.AddDbContext<vNextChinaContext>(x => x.UseInMemoryDatabase())
.AddInMemoryDatabase();
}
services.AddIdentity<User, IdentityRole<long>>(x =>
{
x.Password.RequireDigit = false;
x.Password.RequiredLength = 0;
x.Password.RequireLowercase = false;
x.Password.RequireNonLetterOrDigit = false;
x.Password.RequireUppercase = false;
x.User.AllowedUserNameCharacters = null;
})
.AddEntityFrameworkStores<vNextChinaContext, long>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddSmartUser<User, long>();
services.AddSmartCookies();
services.AddAntiXss();
services.AddSmtpEmailSender("smtp.qq.com", 25, "vNext China", "[email protected]", "911574351", "XXX");
services.AddAesCrypto();
services.AddEFNodeProvider<vNextChinaContext>();
services.AddSignalR();
}
示例14: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = null;
});
// Add EF services to the service container
services.AddEntityFramework()
.AddEntityFrameworkSqlite()
.AddDbContext<MusicStoreContext>(options => {
options.UseSqlite("Data Source=music-db.sqlite");
});
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MusicStoreContext>()
.AddDefaultTokenProviders();
// Configure Auth
services.Configure<AuthorizationOptions>(options =>
{
options.AddPolicy("app-ManageStore", new AuthorizationPolicyBuilder().RequireClaim("app-ManageStore", "Allowed").Build());
});
Mapper.CreateMap<AlbumChangeDto, Album>();
Mapper.CreateMap<Album, AlbumChangeDto>();
Mapper.CreateMap<Album, AlbumResultDto>();
Mapper.CreateMap<AlbumResultDto, Album>();
Mapper.CreateMap<Artist, ArtistResultDto>();
Mapper.CreateMap<ArtistResultDto, Artist>();
Mapper.CreateMap<Genre, GenreResultDto>();
Mapper.CreateMap<GenreResultDto, Genre>();
}
示例15: ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
//cambiar dependiendo los contextos y cadenas
//de conexión que tengas
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:ShellDBContextConnection"]));
services.AddScoped<SeedContext>();
//services.AddEntityFramework()
// .AddSqlServer()
// .AddDbContext<Context>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}