Skip to content

Commit baeb8dc

Browse files
committed
Fix workflow, Fix warnings
1 parent b9310a0 commit baeb8dc

File tree

5 files changed

+57
-27
lines changed

5 files changed

+57
-27
lines changed

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env:
1111
version: '1.0.${{ github.run_number }}'
1212
dotnetVersion: '8'
1313
repoUrl: ${{ github.server_url }}/${{ github.repository }}
14-
vsixPath: ${{ github.workspace }}/src/bin/Release/net8.0-windows8.0/EFCoreViewer.vsix
14+
vsixPath: ${{ github.workspace }}/src/EFCoreViewer/bin/Release/net8.0-windows8.0/EFCoreViewer.vsix
1515

1616
jobs:
1717
build:

src/IQueryableObjectSource/SqlExtensions.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace IQueryableObjectSource;
1111

1212
public static class SqlExtensions
1313
{
14-
private static object Private(this object obj, string privateField)
14+
private static object? Private(this object obj, string privateField)
1515
=> obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
1616

17-
private static T Private<T>(this object obj, string privateField)
18-
=> (T)obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
17+
private static T? Private<T>(this object obj, string privateField)
18+
=> (T?)obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
1919

2020
/// <summary>
2121
/// Gets a SQL statement from an IQueryable
@@ -26,11 +26,34 @@ public static string ToQueryString(this IQueryable query)
2626
{
2727
var enumerator = query.Provider.Execute<IEnumerable>(query.Expression).GetEnumerator();
2828
var relationalCommandCache = enumerator.Private("_relationalCommandCache");
29+
30+
if (relationalCommandCache == null)
31+
{
32+
return string.Empty;
33+
}
34+
2935
var selectExpression = relationalCommandCache.Private<SelectExpression>("_selectExpression");
3036
var factory = relationalCommandCache.Private<IQuerySqlGeneratorFactory>("_querySqlGeneratorFactory");
37+
38+
if (factory == null)
39+
{
40+
return string.Empty;
41+
}
42+
3143
var relationalQueryContext = enumerator.Private<RelationalQueryContext>("_relationalQueryContext");
44+
45+
if (relationalQueryContext == null)
46+
{
47+
return string.Empty;
48+
}
49+
3250
var parameterValueBasedSelectExpressionOptimizer = relationalCommandCache.Private<ParameterValueBasedSelectExpressionOptimizer>("_parameterValueBasedSelectExpressionOptimizer");
3351

52+
if (parameterValueBasedSelectExpressionOptimizer == null)
53+
{
54+
return string.Empty;
55+
}
56+
3457
(selectExpression, _) = parameterValueBasedSelectExpressionOptimizer.Optimize(selectExpression, relationalQueryContext.ParameterValues);
3558

3659
var sqlGenerator = factory.Create();
@@ -59,7 +82,9 @@ private static string GetActualValue(object value)
5982
var type = value.GetType();
6083

6184
if (type.IsNumeric())
62-
return value.ToString();
85+
{
86+
return value.ToString() ?? string.Empty;
87+
}
6388

6489
if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
6590
{
@@ -76,21 +101,22 @@ private static string GetActualValue(object value)
76101
return $"'{value}'";
77102
}
78103

79-
private static bool IsNullable(this Type type)
80-
{
81-
return
82-
type != null &&
83-
type.IsGenericType &&
84-
type.GetGenericTypeDefinition() == typeof(Nullable<>);
85-
}
104+
private static bool IsNullable(this Type? type)
105+
=> type != null &&
106+
type.IsGenericType &&
107+
type.GetGenericTypeDefinition() == typeof(Nullable<>);
86108

87-
private static bool IsNumeric(this Type type)
109+
private static bool IsNumeric(this Type? type)
88110
{
89111
if (IsNullable(type))
90-
type = Nullable.GetUnderlyingType(type);
112+
{
113+
type = Nullable.GetUnderlyingType(type!);
114+
}
91115

92116
if (type == null || type.IsEnum)
117+
{
93118
return false;
119+
}
94120

95121
return Type.GetTypeCode(type) switch
96122
{

test/Blog.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace EFCoreViewer.Test;
22

3-
public class Blog
3+
public record Blog
44
{
5-
public int BlogId { get; set; }
6-
public string Url { get; set; }
5+
public int BlogId { get; init; }
76

8-
public List<Post> Posts { get; } = new();
7+
public string Url { get; init; } = string.Empty;
8+
9+
public List<Post> Posts { get; } = [];
910
}

test/BloggingContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ namespace EFCoreViewer.Test;
44

55
public class BloggingContext : DbContext
66
{
7-
public DbSet<Blog> Blogs { get; set; }
8-
public DbSet<Post> Posts { get; set; }
7+
public DbSet<Blog>? Blogs { get; set; }
8+
public DbSet<Post>? Posts { get; set; }
99

1010
public string DbPath { get; }
1111

1212
public BloggingContext()
1313
{
1414
var folder = Environment.SpecialFolder.LocalApplicationData;
1515
var path = Environment.GetFolderPath(folder);
16-
DbPath = System.IO.Path.Join(path, "blogging.db");
16+
DbPath = Path.Join(path, "blogging.db");
1717
}
1818

1919
// The following configures EF to create a Sqlite database file in the

test/Post.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
namespace EFCoreViewer.Test;
22

3-
public class Post
3+
public record Post
44
{
5-
public int PostId { get; set; }
6-
public string Title { get; set; }
7-
public string Content { get; set; }
5+
public int PostId { get; init; }
86

9-
public int BlogId { get; set; }
10-
public Blog Blog { get; set; }
7+
public string Title { get; init; } = string.Empty;
8+
9+
public string Content { get; init; } = string.Empty;
10+
11+
public int BlogId { get; init; }
12+
13+
public required Blog Blog { get; init; }
1114
}

0 commit comments

Comments
 (0)