Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify to fix issue for #2076, Containment expand problem #2083

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="EntityFramework" Version="6.4.0" />
<!--<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.0" />-->
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using AspNetCore3xEndpointSample.Web.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;


namespace AspNetCore3xEndpointSample.Web.Controllers
{
{/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to keep the commented out version?

public class CustomersController : ODataController
{
private readonly CustomerOrderContext _context;
Expand Down Expand Up @@ -87,4 +87,85 @@ public IActionResult Get(int key)
return Ok(_context.Customers.FirstOrDefault(c => c.Id == key));
}
}
*/
public class CustomersController : ODataController
{
private readonly IList<Customer> _customers;

public CustomersController(/*CustomerOrderContext context*/)
{
_customers = new List<Customer>
{
new Customer
{
ID = 123,
CustomerReferrals = new List<CustomerReferral>
{
new CustomerReferral { ID = 8, CustomerID = 123, ReferredCustomerID = 8},
new CustomerReferral { ID = 9, CustomerID = 123, ReferredCustomerID = 9},
},
Phones = Enumerable.Range(0, 2).Select(e =>
new CustomerPhone { ID = 11, CustomerID = 123, Formatted = new CustomerPhoneNumberFormatted { CustomerPhoneNumberID = 17, FormattedNumber = "171" } }
).ToList()
},
new Customer
{
ID = 456,
CustomerReferrals = new List<CustomerReferral>
{
new CustomerReferral { ID = 18, CustomerID = 456, ReferredCustomerID = 8},
new CustomerReferral { ID = 19, CustomerID = 456, ReferredCustomerID = 9},
},
Phones = Enumerable.Range(0, 2).Select(e =>
new CustomerPhone { ID = 21 + e, CustomerID = 456 + e, Formatted = new CustomerPhoneNumberFormatted { CustomerPhoneNumberID = 17 + e, FormattedNumber = "abc" } }
).ToList()
},
//new Customer
//{
// Name = "Peter",
// HomeAddress = new Address { City = "Hollewye", Street = "Main St NE"},
// FavoriteAddresses = new List<Address>
// {
// new Address { City = "R4mond", Street = "546 NE"},
// new Address { City = "R4d", Street = "546 AVE"},
// },
// Order = new Order { Title = "Jichan" },
// Orders = Enumerable.Range(0, 2).Select(e => new Order { Title = "ijk" + e }).ToList()
//},
};

foreach (var customer in _customers)
{
foreach (var refed in customer.CustomerReferrals)
{
refed.ReferredCustomer = customer;
refed.Customer = customer;
}
}

}

[EnableQuery]
public IActionResult Get()
{
// Be noted: without the NoTracking setting, the query for $select=HomeAddress with throw exception:
// A tracking query projects owned entity without corresponding owner in result. Owned entities cannot be tracked without their owner...
// _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

return Ok(_customers);
}

[EnableQuery]
public IActionResult Get(int key)
{
return Ok(_customers.FirstOrDefault(c => c.ID == key));
}

[EnableQuery(MaxExpansionDepth = 5)]
public IActionResult GetCustomerReferrals(int key)
{
var c = _customers.FirstOrDefault(c => c.ID == key);
return Ok(c.CustomerReferrals);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace AspNetCore3xEndpointSample.Web.Models
{
{/*
Copy link
Member

@mikepizzo mikepizzo Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this commented out? Is it no longer used? Just remove?

public class Customer
{
public int Id { get; set; }
Expand Down Expand Up @@ -35,5 +34,5 @@ public class Address
public string City { get; set; }

public string Street { get; set; }
}
}*/
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore;

using System.Data.Entity;

namespace AspNetCore3xEndpointSample.Web.Models
{
public class CustomerOrderContext : DbContext
{
public CustomerOrderContext(DbContextOptions<CustomerOrderContext> options)
: base(options)
//public CustomerOrderContext(DbContextOptions<CustomerOrderContext> options)
// : base(options)
//{
//}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed?


public CustomerOrderContext(string connectString)
: base(connectString)
{
}

public DbSet<Customer> Customers { get; set; }

public DbSet<Order> Orders { get; set; }
// public DbSet<Order> Orders { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?


protected override void OnModelCreating(ModelBuilder modelBuilder)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().OwnsOne(c => c.HomeAddress).WithOwner();
modelBuilder.Entity<CustomerPhone>().HasOptional(a => a.Formatted).WithRequired();
modelBuilder.Entity<Customer>()
.HasMany(c => c.CustomerReferrals)
.WithRequired(c => c.Customer)
.HasForeignKey(c => c.CustomerID);
}
}
}
58 changes: 56 additions & 2 deletions samples/AspNetCore3xEndpointSample.Web/Models/EdmModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace AspNetCore3xEndpointSample.Web.Models
{
Expand All @@ -15,13 +18,64 @@ public static IEdmModel GetEdmModel()
if (_edmModel == null)
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
builder.EntitySet<Order>("Orders");
var customers = builder.EntitySet<Customer>("Customers");
customers.Binding.HasManyPath(c => c.CustomerReferrals, true).HasRequiredBinding(r => r.ReferredCustomer, "Customers");
// builder.EntitySet<Order>("Orders");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

_edmModel = builder.GetEdmModel();
}

return _edmModel;
}

}

public class Customer
{
[Key]
public int ID { get; set; }

[Contained]
public virtual ICollection<CustomerReferral> CustomerReferrals { get; set; }

[Contained]
public virtual ICollection<CustomerPhone> Phones { get; set; }
}

public class CustomerReferral
{
[Key]
public int ID { get; set; }

public int CustomerID { get; set; }

public int ReferredCustomerID { get; set; }

[Required]
[ForeignKey(nameof(CustomerID))]
public virtual Customer Customer { get; set; }

[Required]
[ForeignKey(nameof(ReferredCustomerID))]
public virtual Customer ReferredCustomer { get; set; }
}

public class CustomerPhone
{
[Key]
public int ID { get; set; }

[Editable(false)]
public int CustomerID { get; set; }

[Contained]
public virtual CustomerPhoneNumberFormatted Formatted { get; set; }
}

public class CustomerPhoneNumberFormatted
{
[Key]
public int CustomerPhoneNumberID { get; set; }

public string FormattedNumber { get; set; }
}
}
9 changes: 5 additions & 4 deletions samples/AspNetCore3xEndpointSample.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using AspNetCore3xEndpointSample.Web.Models;
using Microsoft.AspNet.OData.Batch;
using Microsoft.AspNet.OData.Extensions;
Expand All @@ -11,12 +9,13 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OData;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;

namespace AspNetCore3xEndpointSample.Web
{
Expand All @@ -32,7 +31,8 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<CustomerOrderContext>(opt => opt.UseLazyLoadingProxies().UseInMemoryDatabase("CustomerOrderList"));
//services.AddDbContext<CustomerOrderContext>(opt => opt.UseLazyLoadingProxies().UseInMemoryDatabase("CustomerOrderList"));
//services.AddScoped<CustomerOrderContext>(_ => new CustomerOrderContext(Configuration.GetConnectionString("DefaultConnection")));
services.AddOData();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out lines?

services.AddRouting();
}
Expand All @@ -54,6 +54,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
endpoints.Expand();
endpoints.MapODataRoute(
"nullPrefix", null,
b =>
Expand Down
3 changes: 3 additions & 0 deletions samples/AspNetCore3xEndpointSample.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EF6MVCCore;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.AspNet.OData.Shared/Query/ODataQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ private void Initialize(ODataQueryContext context)

_queryOptionParser = new ODataQueryOptionParser(
context.Model,
context.ElementType,
context.NavigationSource,
context.Path.Path,
normalizedQueryParameters);

// Note: the context.RequestContainer must be set by the ODataQueryOptions constructor.
Expand Down