Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kostov committed Mar 4, 2019
2 parents 2cc06d0 + e8f2c77 commit 8de81b4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/Dapper.Bulk.Shared/PropertiesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public static bool ValidateProperty(PropertyInfo prop)
{
var result = prop.CanWrite;
result = result && (prop.GetSetMethod(true)?.IsPublic ?? false);
result = result && (!prop.GetGetMethod()?.IsVirtual ?? false);
result = result && (!prop.PropertyType.IsClass || prop.PropertyType == typeof(string));
result = result && prop.GetCustomAttributes(true).All(a => a.GetType().Name != "NotMappedAttribute");

Expand Down
20 changes: 10 additions & 10 deletions src/Dapper.Bulk/DapperBulk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,10 @@ private static string GetColumnsStringSqlServer(IEnumerable<PropertyInfo> proper

private static DataTable ToDataTable<T>(IEnumerable<T> data, IList<PropertyInfo> properties)
{
var dataTable = new DataTable();
foreach (var prop in properties)
{
dataTable.Columns.Add(prop.Name);
}

var typeCasts = new Type[properties.Count];
for (var i = 0; i < properties.Count; i++)
{
var isEnum = properties[i].PropertyType.IsEnum;
if (isEnum)
if (properties[i].PropertyType.IsEnum)
{
typeCasts[i] = Enum.GetUnderlyingType(properties[i].PropertyType);
}
Expand All @@ -246,14 +239,21 @@ private static DataTable ToDataTable<T>(IEnumerable<T> data, IList<PropertyInfo>
}
}

var dataTable = new DataTable();
for (var i = 0; i < properties.Count; i++)
{
// Nullable types are not supported.
var propertyNonNullType = Nullable.GetUnderlyingType(properties[i].PropertyType) ?? properties[i].PropertyType;
dataTable.Columns.Add(properties[i].Name, typeCasts[i] == null ? propertyNonNullType : typeCasts[i]);
}

foreach (var item in data)
{
var values = new object[properties.Count];
for (var i = 0; i < properties.Count; i++)
{
var value = properties[i].GetValue(item, null);
var castToType = typeCasts[i];
values[i] = castToType == null ? value : Convert.ChangeType(value, castToType);
values[i] = typeCasts[i] == null ? value : Convert.ChangeType(value, typeCasts[i]);
}

dataTable.Rows.Add(values);
Expand Down
63 changes: 63 additions & 0 deletions tests/Dapper.Bulk.Tests/GuidTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using FluentAssertions;
using Xunit;

namespace Dapper.Bulk.Tests
{
public class GuidTests : SqlServerTestSuite
{
[Fact]
public void InsertBulk()
{
var data = new List<PETranslationPhrase>();
for (var i = 0; i < 10; i++)
{
data.Add(new PETranslationPhrase {
TranslationId = i,
CultureName = i.ToString(),
Phrase = i.ToString(),
PhraseHash = i % 2 == 0 ? Guid.NewGuid() : (Guid?)null,
RowAddedDateTime = DateTime.UtcNow
});
}

using (var connection = this.GetConnection())
{
connection.Open();
var inserted = connection.BulkInsertAndSelect(data).ToList();
for (var i = 0; i < data.Count; i++)
{
IsValidInsert(inserted[i], data[i]);
}
}
}

private static void IsValidInsert(PETranslationPhrase inserted, PETranslationPhrase toBeInserted)
{
inserted.TranslationId.Should().BePositive();
inserted.CultureName.Should().Be(toBeInserted.CultureName);
inserted.Phrase.Should().Be(toBeInserted.Phrase);
inserted.PhraseHash.Should().Be(toBeInserted.PhraseHash);
inserted.RowAddedDateTime.Should().Be(toBeInserted.RowAddedDateTime);
}

[Table("PE_TranslationPhrase")]
public class PETranslationPhrase
{
[Key]
public virtual int TranslationId { get; set; }

public virtual string CultureName { get; set; }

public virtual string Phrase { get; set; }

public virtual Guid? PhraseHash { get; set; }

public virtual DateTime RowAddedDateTime { get; set; }
}
}
}
10 changes: 10 additions & 0 deletions tests/Dapper.Bulk.Tests/SqlServerTestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ [10_Name] NVARCHAR(100) NULL
[Id] BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[10_Name] NVARCHAR(100) NULL
);");

connection.Execute(
$@"{DropTable("PE_TranslationPhrase")}
CREATE TABLE [PE_TranslationPhrase](
[TranslationId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[CultureName] NVARCHAR(100) NOT NULL,
[Phrase] NVARCHAR(100) NOT NULL,
[PhraseHash] uniqueidentifier NULL,
[RowAddedDateTime] DATETIME2 NOT NULL DEFAULT(GETDATE())
);");
}
}
}
Expand Down

0 comments on commit 8de81b4

Please sign in to comment.