Nullable Owned types in EF Core

c# entity-framework-core

Question

I my case I want to store an address but it has to be optional.

My mapping lookes like this:

map.OwnsOne(x => x.Address, cb => cb.OwnsOne(l => l.Location));

But when comitting my DbContext with Address as null iam getting this error:

InvalidOperationException: The entity of 'Member' is sharing the table 'Members' with 'Member.Address#StreetAddress', but there is no entity of this type with the same key value 'Id:-2147480644' that has been marked as 'Added'.

I then instantiated the Address and Location from the constructors, and now I can save the entity. But when fetching the data again I also gets an instantiated Address, where i really wanted a null value.

Is it not possible to make nullable Owned Types ?

1
5
1/2/2018 3:31:44 PM

Popular Answer

Is it not possible to make nullable Owned Types?

As of EF Core 3, this is now possible 🎉.

all dependents are now optional. (Shipping in preview 4): Source


Sample Code:

static void Main(string[] args)
{
  using (var context = new OwnedEntityContext())
  {
    context.Add(new DetailedOrder
    {
      Status = OrderStatus.Pending,
      OrderDetails = new OrderDetails
      {
        ShippingAddress = new StreetAddress
        {
          City = "London",
          Street = "221 B Baker St"
        }
        //testing 3.0: "Yes, all dependents are now optional"
        //reference: https://github.com/aspnet/EntityFrameworkCore/issues/9005#issuecomment-477741082
        //NULL Owned Type Testing
        //BillingAddress = new StreetAddress
        //{
        //    City = "New York",
        //    Street = "11 Wall Street"
        //}
      }
    });
    context.SaveChanges();
  }
  //read test
  using (var context = new OwnedEntityContext())
  {
    #region DetailedOrderQuery
    var order = context.DetailedOrders.First(o => o.Status == OrderStatus.Pending);
    Console.Write("NULL Owned Type Test, Is Billing Address NULL?");
    //PRINTS FALSE
    Console.WriteLine($"{order.OrderDetails.BillingAddress == null}");
    #endregion
  }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    #region OwnsOneNested
    modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
    {
        od.OwnsOne(c => c.BillingAddress);
        od.OwnsOne(c => c.ShippingAddress);
    });
    #endregion

    #region OwnsOneTable
    modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
    {
        od.OwnsOne(c => c.BillingAddress);
        od.OwnsOne(c => c.ShippingAddress);
        od.ToTable("OrderDetails");
        //Exception message:Microsoft.Data.SqlClient.SqlException:
        //'Cascading foreign key 'FK_OrderDetails_DetailedOrders_OrderId' cannot
        //be created where the referencing column 'OrderDetails.OrderId' is an identity column.
        //Could not create constraint or index. See previous errors.'
        //3.0 bug: https://github.com/aspnet/EntityFrameworkCore/issues/17448#issuecomment-525444101 
        //fixed in 3.1: https://github.com/aspnet/EntityFrameworkCore/pull/17458
        od.Property("OrderId")
            .ValueGeneratedNever();
    });
    #endregion
}
6
8/30/2019 8:35:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow