Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit d27961e

Browse files
author
IharYakimush
authored
Merge pull request #6 from IharYakimush/Issue5
InvalidOperationException: The entity does not have a key defined
2 parents 888ec1b + ddb4cb8 commit d27961e

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Community.OData.Linq.xTests
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
using Community.OData.Linq.xTests.SampleData;
7+
8+
using Microsoft.OData;
9+
10+
using Xunit;
11+
12+
public class ODataTests
13+
{
14+
[Fact]
15+
public void CustomKey()
16+
{
17+
var result = SampleWithCustomKey.CreateQuery().OData().Filter("Name eq 'n1'").ToArray();
18+
19+
Assert.Single(result);
20+
Assert.Equal("n1", result[0].Name);
21+
}
22+
23+
[Fact]
24+
public void WithoutKeyThrowException()
25+
{
26+
Assert.Throws<InvalidOperationException>(() => SampleWithoutKey.CreateQuery().OData());
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Community.OData.Linq.xTests.SampleData
2+
{
3+
using System;
4+
using System.ComponentModel.DataAnnotations;
5+
using System.Linq;
6+
7+
public class SampleWithCustomKey
8+
{
9+
private static readonly SampleWithCustomKey[] items =
10+
{
11+
new SampleWithCustomKey { Name = "n1", DateTime = new DateTime(2018, 1, 26)},
12+
new SampleWithCustomKey { Name = "n2", DateTime = new DateTime(2001, 1, 26)}
13+
};
14+
15+
public static IQueryable<SampleWithCustomKey> CreateQuery()
16+
{
17+
return items.AsQueryable();
18+
}
19+
20+
[Key]
21+
public string Name { get; set; }
22+
23+
public DateTime DateTime { get; set; }
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Community.OData.Linq.xTests.SampleData
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
public class SampleWithoutKey
7+
{
8+
private static readonly SampleWithoutKey[] items =
9+
{
10+
new SampleWithoutKey { Name = "n1", DateTime = new DateTime(2018, 1, 26)},
11+
new SampleWithoutKey { Name = "n2", DateTime = new DateTime(2001, 1, 26)}
12+
};
13+
14+
public static IQueryable<SampleWithoutKey> CreateQuery()
15+
{
16+
return items.AsQueryable();
17+
}
18+
19+
public string Name { get; set; }
20+
21+
public DateTime DateTime { get; set; }
22+
}
23+
}

0 commit comments

Comments
 (0)