public void Insert_Custom_Embedded_ColumnName() { var schema = new DataSchemaBuilder() .AddSqlEntity <Complex_Entity>(cfg => cfg.Field(q => q.Flat.Value, cfg => cfg.ColumnName("Custom")).AutoModel()) .Build(); var sqlDataModel = schema.Sql.SqlEntities.OfType <SqlDataModel <Complex_Entity> >().First(); using (var dataProvider = DataProvider.CreateTestProvider()) { dataProvider.ExecuteNonQuery(Sqlite3QueryExpression.Raw($@" CREATE TABLE {sqlDataModel.StorageModel.DefaultTableName} ( [Value] INTEGER, [Custom] INTEGER, [Flat_Id] INTEGER )" )); var dataSet = new SqlDataSet <Complex_Entity>(sqlDataModel, dataProvider); var entity = new Complex_Entity { Value = 5, Flat = new Flat_Entity { Value = 6 } }; dataSet.Insert(entity).Execute(); using (var result = dataProvider.ExecuteReader(QueryExpression.Select(new[] { QueryExpression.All() }, from: QueryExpression.Table(sqlDataModel.StorageModel.DefaultTableName)))) { Assert.IsTrue(result.HasRows); Assert.IsTrue(result.Read()); Assert.AreEqual(entity.Value, result.GetInt32(0)); Assert.AreEqual(entity.Flat.Value, result.GetInt32(1)); } } }
public void Can_Insert_Entity_With_Null_Member() { var schema = new DataSchemaBuilder() .AddSqlEntity <Complex_Entity>() .Build(); var sqlDataModel = schema.Sql.SqlEntities.OfType <SqlDataModel <Complex_Entity> >().First(); using (var dataProvider = DataProvider.CreateTestProvider()) { dataProvider.ExecuteNonQuery(Sqlite3QueryExpression.Raw($@" CREATE TABLE {sqlDataModel.StorageModel.DefaultTableName} ( [Value] INTEGER, [Flat_Value] INTEGER, [Flat_Id] INTEGER )" )); var dataSet = new SqlDataSet <Complex_Entity>(sqlDataModel, dataProvider); var entity = new Complex_Entity { Value = 5 }; dataSet.Insert(entity).Execute(); using (var result = dataProvider.ExecuteReader(QueryExpression.Select(new[] { QueryExpression.All() }, from: QueryExpression.Table(sqlDataModel.StorageModel.DefaultTableName)))) { Assert.IsTrue(result.HasRows); Assert.IsTrue(result.Read()); Assert.AreEqual(entity.Value, result.GetInt32(0)); Assert.AreEqual(0, result.GetInt32(1)); } } }
public void Insert_Complex_Entity_With_Relationship() { var schema = new DataSchemaBuilder() .AddSqlEntity <Flat_Entity>() .AddSqlEntity <Complex_Entity>() .Build(); var flatDataModel = schema.Sql.SqlEntities.OfType <SqlDataModel <Flat_Entity> >().First(); var complexDataModel = schema.Sql.SqlEntities.OfType <SqlDataModel <Complex_Entity> >().First(); using (var dataProvider = DataProvider.CreateTestProvider()) { dataProvider.ExecuteNonQuery(Sqlite3QueryExpression.Raw($@" CREATE TABLE {flatDataModel.StorageModel.DefaultTableName} ( [Value] INTEGER, [Id] INTEGER PRIMARY KEY AUTOINCREMENT )" )); dataProvider.ExecuteNonQuery(Sqlite3QueryExpression.Raw($@" CREATE TABLE {complexDataModel.StorageModel.DefaultTableName} ( [Value] INTEGER, [Flat_Id] INTEGER )" )); var flatDataSet = new SqlDataSet <Flat_Entity>(flatDataModel, dataProvider); var complexDataSet = new SqlDataSet <Complex_Entity>(complexDataModel, dataProvider); // the ID here isn't written to the INSERT query because the ORM see's it as server generated (which it should be) var flatEntity = new Flat_Entity { Id = 1, Value = 5 }; var complexEntity = new Complex_Entity { Value = 6, Flat = flatEntity }; flatDataSet.Insert(flatEntity).Execute(); complexDataSet.Insert(complexEntity).Execute(); using (var result = dataProvider.ExecuteReader(QueryExpression.Select( new[] { QueryExpression.All() }, from: QueryExpression.Table(complexDataModel.StorageModel.DefaultTableName), joins: new[] { QueryExpression.Join( QueryExpression.Table(flatDataModel.StorageModel.DefaultTableName), QueryExpression.AreEqual( QueryExpression.Column("Flat_Id", QueryExpression.Table(complexDataModel.StorageModel.DefaultTableName)), QueryExpression.Column("Id", QueryExpression.Table(flatDataModel.StorageModel.DefaultTableName)) ), JoinDirection.Left ) }))) { Assert.IsTrue(result.HasRows); Assert.IsTrue(result.Read()); Assert.AreEqual(complexEntity.Value, result.GetInt32(0)); Assert.AreEqual(complexEntity.Flat.Id, result.GetInt32(1)); Assert.AreEqual(flatEntity.Value, result.GetInt32(2)); Assert.AreEqual(flatEntity.Id, result.GetInt32(3)); } } }