Skip to content

Commit

Permalink
Modify to fix issue for #2076, Containment expand problem
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Mar 12, 2020
1 parent e5410e8 commit 6667e20
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 24 deletions.
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
{
{/*
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
{
{/*
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)
//{
//}

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

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

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

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");
_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();
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.OData.Core" Version="7.6.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.6.1" />
<PackageReference Include="Microsoft.Spatial" Version="7.6.1" />
<PackageReference Include="Microsoft.OData.Core" Version="7.6.2" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.6.2" />
<PackageReference Include="Microsoft.Spatial" Version="7.6.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 6667e20

Please sign in to comment.