/// <summary> /// Mains. /// </summary> /// <param name="args">The arguments.</param> static void Main(string[] args) { CommandBuilderFactory.Assign(mapper => new SqliteCommandBuilder(mapper)); string cs = "URI=file:test.db"; var connection = new SQLiteConnection(cs); connection.Open(); if (!connection.TableExists("Users")) { using (var uow = new AdoNetUnitOfWork(connection)) { uow.Execute( "CREATE TABLE Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, FirstName TEXT, LastName text, CreatedAtUtc INTEGER)"); uow.SaveChanges(); } } var users = connection.ToList <User>("firstName = @FirstName", new { FirstName = "Gau%" }); try { var first = connection.First <User>(new { Id = 1 }); } catch (EntityNotFoundException x) { Console.WriteLine("Expected to find user, since First was used instead of FirstOrDefault. User friendly error message: " + x.Message); } // clear old data using (var uow = new AdoNetUnitOfWork(connection)) { using (var cmd = uow.CreateCommand()) { cmd.CommandText = "SELECT * FROM Users"; cmd.AddParameter("id", "983498043903"); foreach (var entity in cmd.ToEnumerable <User>()) { Console.WriteLine(entity.FirstName); } } uow.Truncate <User>(); for (int i = 0; i < 100; i++) { uow.Insert(new User { FirstName = "Arne" + i }); } uow.SaveChanges(); } Console.WriteLine("Press ENTER to quit."); Console.ReadLine(); }
/// <summary> /// Free the mapping, no further changes may be made. /// </summary> /// <remarks> /// <para>Called by the mapping provider when it's being added to it.</para> /// </remarks> public void Freeze() { _builder = CommandBuilderFactory.Create(this); foreach (var kvp in _mappings.Where(x => x.Value.IsPrimaryKey)) { _keys.Add(kvp); } }
private int Update(DbCommand command, DataTable table) { using (var builder = new CommandBuilderFactory(_server.Engine).CreateCommandBuilder()) using (var adapter = new AdapterFactory(_server.Engine).CreateAdapter()) { adapter.SelectCommand = command; builder.DataAdapter = adapter; builder.GetUpdateCommand(); return(adapter.Update(table)); } }
public AsyncCommandExtensionsTests() { CommandBuilderFactory.Assign(mapper => new SqliteCommandBuilder(mapper)); _dbFile = Path.GetTempFileName(); var cs = "URI=file:" + _dbFile; _connection = new SQLiteConnection(cs); _connection.Open(); _userTable.Create(_connection); }
public ConnectionExtensionsTests() { CommandBuilderFactory.Assign(mapper => new SqliteCommandBuilder(mapper)); //EntityMappingProvider.Provider = new AssemblyScanningMappingProvider(); _dbFile = Path.GetTempFileName(); var cs = "URI=file:" + _dbFile; _connection = new SQLiteConnection(cs); _connection.Open(); _userTable.Create(_connection); }
static void Main(string[] args) { CommandBuilderFactory.Assign(mapper => new SqliteCommandBuilder(mapper)); string cs = "URI=file:test.db"; var connection = new SQLiteConnection(cs); connection.Open(); if (!connection.TableExists("Users")) { using (var uow = new AdoNetUnitOfWork(connection)) { uow.Execute( "CREATE TABLE Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, FirstName TEXT, LastName text, CreatedAtUtc INTEGER)"); uow.SaveChanges(); } } var users = connection.ToList <User>(new { FirstName = "Gau%" }); var first = connection.First <User>(new { Id = 1 }); // clear old data using (var uow = new AdoNetUnitOfWork(connection)) { using (var cmd = uow.CreateCommand()) { cmd.CommandText = "SELECT * FROM Users"; cmd.AddParameter("id", "983498043903"); foreach (var entity in cmd.ToEnumerable <User>()) { Console.WriteLine(entity.FirstName); } } uow.Truncate <User>(); for (int i = 0; i < 100; i++) { uow.Insert(new User { FirstName = "Arne" + i }); } uow.SaveChanges(); } }
public void ApplyOrderedParametersOnlyIfExactLengthMatch() { var builder = new CommandBuilderFactory(commandText: @"SELECT 1") { { "@param", DbType.String }, { "@param2", DbType.String, p => p.Size = 128 } }.Build(); var mocks = CommandBuilderMocks.SetupFor(new CommandSetup { { builder, (mockCmd, record) => { } }, }); Assert.Throws <InvalidOperationException>(() => builder.BuildFrom(mocks.Connection.Object, new[] { "param" })); Assert.Throws <InvalidOperationException>(() => builder.BuildFrom(mocks.Connection.Object, new[] { "param", "param2", "param3" })); }
public AsyncUnitOfWorkExtensionsTests() { CommandBuilderFactory.Assign(mapper => new SqliteCommandBuilder(mapper)); var provider = new AssemblyScanningMappingProvider(); provider.Scan(Assembly.GetExecutingAssembly()); EntityMappingProvider.Provider = provider; _dbFile = Path.GetTempFileName(); var cs = "URI=file:" + _dbFile; _connection = new SQLiteConnection(cs); _connection.Open(); _uow = new AdoNetUnitOfWork(_connection); _userTable.Create(_connection); }
/// <summary> /// Free the mapping, no further changes may be made. /// </summary> /// <remarks> /// <para>Called by the mapping provider when the mapping have been added to it.</para> /// <para> /// The purpose is to allow the mapping implementations to do post process once the mappings have been fully /// configured. /// </para> /// </remarks> public void Freeze() { _builder = CommandBuilderFactory.Create(this); foreach (var kvp in Properties.Where(x => x.Value.IsPrimaryKey)) { _keys.Add(kvp); } if (_keys.Count != 0) { return; } if (Properties.ContainsKey("Id")) { _keys.Add("Id", Properties["Id"]); } }
public void ApplyOrderedParameters() { var builder = new CommandBuilderFactory(commandText: @"SELECT 1") { { "@param", DbType.String }, { "@param2", DbType.String, p => p.Size = 128 } }.Build(); var mocks = CommandBuilderMocks.SetupFor(new CommandSetup { { builder, (mockCmd, record) => { } }, }); var command = builder.BuildFrom(mocks.Connection.Object, new[] { "param", "param2" }); Assert.Collection(command.Parameters.OfType <DbParameter>(), param => { Assert.Equal("@param", param.ParameterName); Assert.Equal("param", param.Value); }, param2 => { Assert.Equal("@param2", param2.ParameterName); Assert.Equal("param2", param2.Value); } ); }
public void AllowCustomizedUnnamedNamedParameters() { var builder = new CommandBuilderFactory(commandText: @"SELECT 1") { { DbType.String }, { DbType.String, p => p.Size = 128 } }.Build(); var mocks = CommandBuilderMocks.SetupFor(new CommandSetup { { builder, (mockCmd, record) => { } }, }); var command = builder.BuildFrom(mocks.Connection.Object); Assert.Collection(command.Parameters.OfType <DbParameter>(), param => { Assert.Equal(DbType.String, param.DbType); Assert.NotEqual(128, param.Size); }, param2 => { Assert.Equal(DbType.String, param2.DbType); Assert.Equal(128, param2.Size); } ); }
public FileSystemNetworkServer(ConnectionFactory connectionFactory, CommandBuilderFactory commandBuilderFactory, CommandProcessorProviderFactory commandProcessorProviderFactory, IPEndPoint localEndPoint) : base(connectionFactory, commandBuilderFactory, commandProcessorProviderFactory, localEndPoint) { Initialize(); }
public FileSystemNetworkServer(ConnectionFactory connectionFactory, CommandBuilderFactory commandBuilderFactory, CommandProcessorProviderFactory commandProcessorProviderFactory, IPAddress address, int port) : base(connectionFactory, commandBuilderFactory, commandProcessorProviderFactory, address, port) { Initialize(); }