Skip to content

Instantly share code, notes, and snippets.

@xuzhg
Last active Nov 28, 2019
Embed
What would you like to do?
BookStoreContext.cs
public class BookStoreContext : DbContext
{
public BookStoreContext(DbContextOptions<BookStoreContext> options)
: base(options)
{
}
public DbSet<Book> Books { get; set; }
public DbSet<Press> Presses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>().OwnsOne(c => c.Location);
}
}
@J0rgeSerran0
Copy link

J0rgeSerran0 commented Jul 5, 2018

From my point of view I think that to be consistent in the sample (https://blogs.msdn.microsoft.com/odatateam/2018/07/03/asp-net-core-odata-now-available/)

public DbSet Press { get; set; }
should be
public DbSet Presses { get; set; }

In other case, BooksController will return an error for

public BooksController(BookStoreContext context)
{
this._db = context;
if (context.Books.Count() == 0)
{
foreach (var b in DataSource.GetBooks())
{
context.Books.Add(b);
context.Presses.Add(b.Press);
}
context.SaveChanges();
}
}

@luritas
Copy link

luritas commented Aug 23, 2018

@J0rgeSerran0 thanks

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

I'm trying to do the same example, but adding pagination. I'm having problems with this:

[HttpGet]
[EnableQuery(PageSize = 2)]
public IActionResult Get()
{
return Ok(_context.Books);
}

the result should be include the "@odata.nextLink", but I've got just the values...
The result I got is:
[
{
"id": 1,
"isbn": "978-0-321-87758-1",
"title": "Essential C#5.0",
"author": "Mark Michaelis",
"price": 59.99,
"location": {
"city": "Redmond",
"street": "156TH AVE NE"
},
"press": null
},
{
"id": 2,
"isbn": "063-6-920-02371-5",
"title": "Enterprise Games",
"author": "Michael Hugos",
"price": 49.99,
"location": {
"city": "Bellevue",
"street": "Main ST"
},
"press": null
}
]

@xuzhg
Copy link
Author

xuzhg commented Nov 27, 2019

@J0rgeSerran0 Thanks. I missed your message and can't see it until today. Updated.

@xuzhg
Copy link
Author

xuzhg commented Nov 27, 2019

@fabriziogelsi Which version are you using?

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

NetCore 2.1 + OData-v4.0 v7.2.2

@xuzhg
Copy link
Author

xuzhg commented Nov 27, 2019

Can you try 7.0 or 7.1 to see the problem is there also?

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

I'm still getting not information about odata.context or odata.dataLink (I want to get this information because I'm learning and trying to do a project with that data, I want to do a pagination by server-side). I've tried with 7.0 and 7.1 right now :s.

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

Response header: (Should be "application/json; odata.metadata=minimal; odata.streaming=true"?) I got:
Content-Type application/json; charset=utf-8

@xuzhg
Copy link
Author

xuzhg commented Nov 27, 2019

@fabriziogelsi I see. Did you add more books into the Datasource? In my sample, there's only two books sample, you add "PageSize=2", the whole data source meets the page size requirement, so the server returns all data and no nextlink added.

If you change to [Pagesize=1], you will get the response:

{
    "@odata.context": "http://localhost:5001/odata/$metadata#Books",
    "value": [
        {
            "Id": 1,
            "ISBN": "978-0-321-87758-1",
            "Title": "Essential C#5.0",
            "Author": "Mark Michaelis",
            "Price": 59.99,
            "Location": {
                "City": "Redmond",
                "Street": "156TH AVE NE"
            }
        }
    ],
    "@odata.nextLink": "http://localhost:5001/odata/Books?$skip=1"
}

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

Yes, I added 5 books. When I do the same than you, the response is:

[ { "id": 1, "isbn": "978-0-321-87758-1", "title": "Essential C#5.0", "author": "Mark Michaelis", "price": 59.99, "location": { "city": "Redmond", "street": "156TH AVE NE" }, "press": { "id": 1, "name": "Addison-Wesley", "email": null, "category": 0 } } ]

Which version NetCore are you using? And OData?. I see all the examples in internet that the response contains all the data, but I can't take the same response, Idk why :S I've just copied your example, and added the "[Enable Query(PageSize =)]" what is working, the pagination, but the other data is not retrieve.

@xuzhg
Copy link
Author

xuzhg commented Nov 27, 2019

Your payload looks a "Normal" JSON Payload, it's not OData JSON Payload. OData JSON Payload includes both control information and entity data. "@odata.context" and "@odata.nextLink" are control data, why does your data not have "@odata.context"? Do you set the metadata level as None? However, your metadata is "odata.metadata=minimal" not "odata.metadata=none". So, it's weird. Please compare your sample with my sample to see what's the difference.

I didn't change my project configuration. I cloned the sample and built/run in the Visual Studio 2019.

@fabriziogelsi
Copy link

fabriziogelsi commented Nov 27, 2019

No. My header's response is: Content-Type application/json; charset=utf-8
I was wondering if I should receive a header like this: "application/json; odata.metadata=minimal; odata.streaming=true".
I will check again, but I've just copied your sample, so I don't understand why is not working :S. I'm using VS2019 too.

@J0rgeSerran0
Copy link

J0rgeSerran0 commented Nov 28, 2019

Great @xuzhg :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment