本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.AddEntityFramework方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddEntityFramework方法的具体用法?C# ServiceCollection.AddEntityFramework怎么用?C# ServiceCollection.AddEntityFramework使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.DependencyInjection.ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.AddEntityFramework方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public void Main(string[] args) {
var connectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Test;Integrated Security=True;Connect Timeout=30;";
IServiceCollection services = new ServiceCollection();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<DataContext>(options => options.UseSqlServer(connectionString));
var serviceProvider = services.BuildServiceProvider();
//serviceProvider.Add
var context = serviceProvider.GetService<DataContext>();
var categoryCount = context.Categories.Count();
Console.WriteLine($"category count is {categoryCount}");
var newCat = new Category {
Name = $"Category {categoryCount + 1}",
Description = $"Category {categoryCount + 1} description"
};
context.Add(newCat);
//context.SaveChanges();
var count = context.SaveChanges();
Console.WriteLine($"change count is {count}");
context.Dispose();
Console.WriteLine("Hello, world!");
}
示例2: ActivityApiControllerTest
public ActivityApiControllerTest()
{
if (_serviceProvider == null)
{
var services = new ServiceCollection();
// Add Configuration to the Container
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables();
IConfiguration configuration = builder.Build();
services.AddSingleton(x => configuration);
// Add EF (Full DB, not In-Memory)
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<AllReadyContext>(options => options.UseInMemoryDatabase());
// Setup hosting environment
IHostingEnvironment hostingEnvironment = new HostingEnvironment();
hostingEnvironment.EnvironmentName = "Development";
services.AddSingleton(x => hostingEnvironment);
_serviceProvider = services.BuildServiceProvider();
}
}
示例3: ConfigureServices
public void ConfigureServices()
{
IServiceProvider mainProv = CallContextServiceLocator.Locator.ServiceProvider;
IApplicationEnvironment appEnv = mainProv.GetService<IApplicationEnvironment>();
IRuntimeEnvironment runtimeEnv = mainProv.GetService<IRuntimeEnvironment>();
ILoggerFactory logFactory = new LoggerFactory();
logFactory.AddConsole(LogLevel.Information);
ServiceCollection sc = new ServiceCollection();
sc.AddInstance(logFactory);
sc.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
sc.AddEntityFramework()
.AddSqlite()
.AddDbContext<StarDbContext>();
sc.AddSingleton<ILibraryManager, LibraryManager>(factory => mainProv.GetService<ILibraryManager>() as LibraryManager);
sc.AddSingleton<ICache, Cache>(factory => new Cache(new CacheContextAccessor()));
sc.AddSingleton<IExtensionAssemblyLoader, ExtensionAssemblyLoader>();
sc.AddSingleton<IStarLibraryManager, StarLibraryManager>();
sc.AddSingleton<PluginLoader>();
sc.AddSingleton(factory => mainProv.GetService<IAssemblyLoadContextAccessor>());
sc.AddInstance(appEnv);
sc.AddInstance(runtimeEnv);
Services = sc;
ServiceProvider = sc.BuildServiceProvider();
}
示例4: Can_get_default_services
public void Can_get_default_services()
{
var services = new ServiceCollection();
services.AddEntityFramework().AddSqlServer();
// Relational
Assert.True(services.Any(sd => sd.ServiceType == typeof(DatabaseBuilder)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(RelationalObjectArrayValueReaderFactory)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(RelationalTypedValueReaderFactory)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(CommandBatchPreparer)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(ModificationCommandComparer)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(GraphFactory)));
// SQL Server dingletones
Assert.True(services.Any(sd => sd.ServiceType == typeof(DataStoreSource)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerSqlGenerator)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlStatementExecutor)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerTypeMapper)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerBatchExecutor)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(ModificationCommandBatchFactory)));
// SQL Server scoped
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerDataStore)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerConnection)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(ModelDiffer)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerMigrationOperationSqlGeneratorFactory)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(SqlServerDataStoreCreator)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(MigrationAssembly)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(HistoryRepository)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(Migrator)));
}
示例5: Main
public void Main(string[] args)
{
var services = new ServiceCollection();
services
.AddEntityFramework()
.AddSqlServer()
.AddDbContext<UserDbContext>()
.AddEntityFrameworkMixins();
var provider = services.BuildServiceProvider();
var context = provider.GetRequiredService<UserDbContext>();
var users = (
from u in context.Users
where u.Mixin<Author>().GooglePlusProfile != null
select u //u.Mixin<Author>()
).ToList();
var user = users.FirstOrDefault();
if (user != null)
{
var author = user.Mixin<Author>();
// You can make changes here:
author.IsAwesome = true;
// Save changes
context.SaveChanges();
}
}
示例6: Can_use_an_existing_closed_connection_test
private static async Task Can_use_an_existing_closed_connection_test(bool openConnection)
{
var serviceCollection = new ServiceCollection();
serviceCollection
.AddEntityFramework()
.AddSqlServer();
var serviceProvider = serviceCollection.BuildServiceProvider();
using (var store = await SqlServerNorthwindContext.GetSharedStoreAsync())
{
var openCount = 0;
var closeCount = 0;
var disposeCount = 0;
using (var connection = new SqlConnection(store.Connection.ConnectionString))
{
if (openConnection)
{
await connection.OpenAsync();
}
connection.StateChange += (_, a) =>
{
if (a.CurrentState == ConnectionState.Open)
{
openCount++;
}
else if (a.CurrentState == ConnectionState.Closed)
{
closeCount++;
}
};
#if !DNXCORE50
connection.Disposed += (_, __) => disposeCount++;
#endif
using (var context = new NorthwindContext(serviceProvider, connection))
{
Assert.Equal(91, await context.Customers.CountAsync());
}
if (openConnection)
{
Assert.Equal(ConnectionState.Open, connection.State);
Assert.Equal(0, openCount);
Assert.Equal(0, closeCount);
}
else
{
Assert.Equal(ConnectionState.Closed, connection.State);
Assert.Equal(1, openCount);
Assert.Equal(1, closeCount);
}
Assert.Equal(0, disposeCount);
}
}
}
示例7: Can_get_default_services
public void Can_get_default_services()
{
var services = new ServiceCollection();
services.AddEntityFramework().AddInMemoryStore();
Assert.True(services.Any(sd => sd.ServiceType == typeof(InMemoryDataStore)));
Assert.True(services.Any(sd => sd.ServiceType == typeof(DataStoreSource)));
}
示例8: GenreMenuComponentTest
public GenreMenuComponentTest()
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddInMemoryStore()
.AddDbContext<MusicStoreContext>();
_serviceProvider = services.BuildServiceProvider();
}
示例9: CreateConfiguration
public static DbContextConfiguration CreateConfiguration()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddEntityFramework().AddSqlServer();
return new DbContext(serviceCollection.BuildServiceProvider(),
new DbContextOptions()
.UseSqlServer("Server=(localdb)\v11.0;Database=SqlServerConnectionTest;Trusted_Connection=True;"))
.Configuration;
}
示例10: CheckoutControllerTest
public CheckoutControllerTest()
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddInMemoryStore()
.AddDbContext<MusicStoreContext>();
_serviceProvider = services.BuildServiceProvider();
}
示例11: CartSummaryComponentTest
public CartSummaryComponentTest()
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());
_serviceProvider = services.BuildServiceProvider();
}
示例12: ConfigureServices
public void ConfigureServices()
{
ServiceCollection sc = new ServiceCollection();
sc.AddEntityFramework()
.AddSqlite()
.AddDbContext<StarDbContext>();
sc.AddTransient<StarDbContext>();
sc.BuildServiceProvider();
}
示例13: CreateContext
public static InMemoryContext CreateContext()
{
var services = new ServiceCollection();
services.AddEntityFramework().AddInMemoryDatabase();
var serviceProvider = services.BuildServiceProvider();
var db = new InMemoryContext();
db.Database.EnsureCreated();
return db;
}
示例14: Create
public static IServiceProvider Create()
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
示例15: AddRelational_does_not_replace_services_already_registered
public void AddRelational_does_not_replace_services_already_registered()
{
var services = new ServiceCollection()
.AddSingleton<IComparer<ModificationCommand>, FakeModificationCommandComparer>();
services.AddEntityFramework().AddRelational();
var serviceProvider = services.BuildServiceProvider();
Assert.IsType<FakeModificationCommandComparer>(serviceProvider.GetRequiredService<IComparer<ModificationCommand>>());
}
开发者ID:aishaloshik,项目名称:EntityFramework,代码行数:11,代码来源:RelationalEntityServicesBuilderExtensionsTest.cs