Skip to content

Commit 5484f6a

Browse files
Issue MikaelEliasson#47 : UpdateAll doesn't work for nvarchar(xxx)
1 parent dc93f6f commit 5484f6a

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void UpdateAll<TEntity>(IEnumerable<TEntity> items, Action<UpdateSpecific
160160
.Select(p => new ColumnMapping {
161161
NameInDatabase = p.ColumnName,
162162
NameOnObject = p.PropertyName,
163-
DataType = p.DataType,
163+
DataType = p.DataTypeFull,
164164
IsPrimaryKey = p.IsPrimaryKey
165165
}).ToList();
166166

EntityFramework.Utilities/EntityFramework.Utilities/MappingHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public class PropertyMapping
9292
public string DataType { get; set; }
9393

9494
public bool IsPrimaryKey { get; set; }
95+
96+
public string DataTypeFull { get; set; }
9597
}
9698

9799
/// <summary>
@@ -172,13 +174,15 @@ public EfMapping(DbContext db)
172174
{
173175
ColumnName = scalar.Column.Name,
174176
DataType = scalar.Column.TypeName,
177+
DataTypeFull = GetFullTypeName(scalar),
175178
PropertyName = path + item.Property.Name,
176179
ForEntityType = t
177180
});
178181
}
179182
};
180183

181-
Func<MappingFragment, Type> getClr = m => {
184+
Func<MappingFragment, Type> getClr = m =>
185+
{
182186
return GetClrTypeFromTypeMapping(metadata, objectItemCollection, m.TypeMapping as EntityTypeMapping);
183187
};
184188

@@ -188,7 +192,7 @@ public EfMapping(DbContext db)
188192
tableMapping.TPHConfiguration = new TPHConfiguration
189193
{
190194
ColumnName = withConditions.First().Fragments[0].Conditions[0].Column.Name,
191-
Mappings = new Dictionary<Type,string>()
195+
Mappings = new Dictionary<Type, string>()
192196
};
193197
foreach (var item in withConditions)
194198
{
@@ -221,6 +225,15 @@ public EfMapping(DbContext db)
221225
}
222226
}
223227

228+
private string GetFullTypeName(ScalarPropertyMapping scalar)
229+
{
230+
if (scalar.Column.TypeName == "nvarchar" || scalar.Column.TypeName == "varchar")
231+
{
232+
return string.Format("{0}({1})", scalar.Column.TypeName, scalar.Column.MaxLength);
233+
}
234+
return scalar.Column.TypeName;
235+
}
236+
224237
private Type GetClrTypeFromTypeMapping(MetadataWorkspace metadata, ObjectItemCollection objectItemCollection, EntityTypeMapping mapping)
225238
{
226239
return GetClrType(metadata, objectItemCollection, mapping.EntityType ?? mapping.IsOfEntityTypes.First());

EntityFramework.Utilities/Tests/FakeDomain/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
3333
modelBuilder.Entity<Person>()
3434
.Map<Person>(m => m.Requires("Type").HasValue("Person"))
3535
.Map<Contact>(m => m.Requires("Type").HasValue("Contact"));
36+
37+
modelBuilder.Entity<BlogPost>().Property(x => x.ShortTitle).HasMaxLength(100);
3638
}
3739

3840
public static Context Sql()

EntityFramework.Utilities/Tests/Models/BlogPost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class BlogPost
99
{
1010
public int ID { get; set; }
1111
public string Title { get; set; }
12+
public string ShortTitle { get; set; }
1213
public DateTime Created { get; set; }
1314
public int Reads { get; set; }
1415
public AuthorInfo Author { get; set; }

EntityFramework.Utilities/Tests/Models/ReorderedBlogPost.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Tests.FakeDomain.Models
88
public class ReorderedBlogPost
99
{
1010
public int ID { get; set; }
11+
public string ShortTitle { get; set; }
1112
public DateTime Created { get; set; }
1213
public string Title { get; set; } //<--- Reversed order of this and created for Batch Insert testing
1314
public int Reads { get; set; }

EntityFramework.Utilities/Tests/UpdateBulkTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ public void UpdateBulk_UpdatesAll()
3434
}
3535
}
3636

37+
[TestMethod]
38+
public void UpdateBulk_CanUpdateNvarcharWithLength()
39+
{
40+
Setup();
41+
42+
using (var db = Context.Sql())
43+
{
44+
var posts = db.BlogPosts.ToList();
45+
foreach (var post in posts)
46+
{
47+
post.ShortTitle = post.Title.Replace("1", "4").Replace("2", "8").Replace("3", "12");
48+
}
49+
EFBatchOperation.For(db, db.BlogPosts).UpdateAll(posts, spec => spec.ColumnsToUpdate(p => p.ShortTitle));
50+
}
51+
52+
using (var db = Context.Sql())
53+
{
54+
var posts = db.BlogPosts.OrderBy(b => b.ID).ToList();
55+
Assert.AreEqual("T4", posts[0].ShortTitle);
56+
Assert.AreEqual("T8", posts[1].ShortTitle);
57+
Assert.AreEqual("T12", posts[2].ShortTitle);
58+
}
59+
}
60+
3761
private static void Setup()
3862
{
3963
using (var db = Context.Sql())

0 commit comments

Comments
 (0)