Skip to content

Commit d1f90bd

Browse files
Closes #9
1 parent 3751e6c commit d1f90bd

File tree

6 files changed

+63
-32
lines changed

6 files changed

+63
-32
lines changed

src/DotNetToolkit.Repository/Properties/Resources.Designer.cs

Lines changed: 27 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="EntityKeyNotFound" xml:space="preserve">
121+
<value>No entity found in the repository with the '{0}' key.</value>
122+
</data>
120123
<data name="EntityRequiresPrimaryKey" xml:space="preserve">
121124
<value>The instance of entity type '{0}' requires a primary key to be defined.</value>
122125
</data>

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
{
33
using FetchStrategies;
44
using Helpers;
5+
using Properties;
56
using Queries;
67
using Specifications;
78
using System;
89
using System.Collections.Generic;
10+
using System.Globalization;
911
using System.Linq;
1012
using System.Linq.Expressions;
1113

@@ -166,7 +168,7 @@ protected virtual bool GetExist(ISpecification<TEntity> criteria)
166168
// https://github.com/SharpRepository/SharpRepository/blob/develop/SharpRepository.Repository/RepositoryBase.cs
167169
protected virtual ISpecification<TEntity> GetByPrimaryKeySpecification(TKey key, IFetchStrategy<TEntity> fetchStrategy = null)
168170
{
169-
var propInfo = ConventionHelper.GetPrimaryKeyPropertyInfo(GetType());
171+
var propInfo = ConventionHelper.GetPrimaryKeyPropertyInfo(typeof(TEntity));
170172
var parameter = Expression.Parameter(typeof(TEntity), "x");
171173
var lambda = Expression.Lambda<Func<TEntity, bool>>(
172174
Expression.Equal(
@@ -302,7 +304,7 @@ public void Delete(TKey key)
302304

303305
var entity = Get(key);
304306
if (entity == null)
305-
throw new InvalidOperationException($"No entity found in the repository with the '{key}' key.");
307+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyNotFound, key));
306308

307309
DeleteItem(entity);
308310
SaveChanges();

test/DotNetToolkit.Repository.Integration.Test/Data/TestDbContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@ public class TestDbContext : DbContext
1010
public TestDbContext(DbConnection connection) : base(connection, true)
1111
{
1212
}
13+
14+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
15+
{
16+
modelBuilder.Entity<Customer>()
17+
.HasOptional(s => s.Address)
18+
.WithRequired(ad => ad.Customer);
19+
}
1320
}
1421
}

test/DotNetToolkit.Repository.Integration.Test/Data/TestEntity.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,15 @@ public class Customer
44
{
55
public int Id { get; set; }
66
public string Name { get; set; }
7+
public CustomerAddress Address { get; set; }
8+
}
9+
10+
public class CustomerAddress
11+
{
12+
public int Id { get; set; }
13+
public string Street { get; set; }
14+
public string City { get; set; }
15+
public string State { get; set; }
16+
public Customer Customer { get; set; }
717
}
818
}

test/DotNetToolkit.Repository.Integration.Test/EfRepositoryTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
{
33
using Data;
44
using EntityFramework;
5+
using FetchStrategies;
56
using Queries;
67
using Specifications;
8+
using System;
79
using System.Collections.Generic;
810
using System.Linq;
911
using System.Threading.Tasks;
10-
using FetchStrategies;
1112
using Xunit;
1213

1314
public class EfRepositoryTests
@@ -912,14 +913,19 @@ public void Get()
912913
int key = 1;
913914
const string name = "Random Name";
914915

916+
var fetchStrategy = new FetchStrategy<Customer>();
917+
fetchStrategy.Include(x => x.Address);
918+
915919
var entity = new Customer { Id = key, Name = name };
916920
var repo = new EfRepository<Customer>(context);
917921

918922
Assert.Null(repo.Get(key));
923+
Assert.Null(repo.Get(key, fetchStrategy));
919924

920925
repo.Add(entity);
921926

922927
Assert.NotNull(repo.Get(key));
928+
Assert.NotNull(repo.Get(key, fetchStrategy));
923929
}
924930
}
925931

@@ -931,14 +937,19 @@ public async Task Get_Async()
931937
int key = 1;
932938
const string name = "Random Name";
933939

940+
var fetchStrategy = new FetchStrategy<Customer>();
941+
fetchStrategy.Include(x => x.Address);
942+
934943
var entity = new Customer { Id = key, Name = name };
935944
var repo = new EfRepository<Customer>(context);
936945

937946
Assert.Null(await repo.GetAsync(key));
947+
Assert.Null(await repo.GetAsync(key, fetchStrategy));
938948

939949
await repo.AddAsync(entity);
940950

941951
Assert.NotNull(await repo.GetAsync(key));
952+
Assert.NotNull(await repo.GetAsync(key, fetchStrategy));
942953
}
943954
}
944955

0 commit comments

Comments
 (0)