none
EF Core 3: owned property insert fails when SaveChanges() RRS feed

  • Question

  • My problem

    Inserting an entity with an owned property fails.

    I have a `Restaurant` entity with an `Address` owned property. When I try to create an new entity and insert into the database, an exception is thrown at `SaveChanges`:

    Cannot insert the value NULL into column 'RestaurantId', table 'AppRefDB.dbo.Addresses'; column does not allow nulls. INSERT fails.


    I am using EF Core 3.
    Update works fine, I just have a problem with creating an new restaurant/address.

    What I did

    My table `Addresses` looks like this:

    CREATE TABLE [dbo].[Addresses]
    (
    	[RestaurantId] INT NOT NULL, 
    	[Number] NVARCHAR(8) NULL, 
    	[Street] NVARCHAR(150) NOT NULL, 
    	[Zip] NVARCHAR(10) NOT NULL, 
    	[Town] NVARCHAR(50) NOT NULL, 
    	[Site] NVARCHAR(150) NULL ,
    	
    	CONSTRAINT [PK_Addresses] 
    		PRIMARY KEY ([RestaurantId]),
    	CONSTRAINT [FK_Address_Restaurants_RestaurantId] 
    		FOREIGN KEY ([RestaurantId]) REFERENCES [Restaurants] ([Id]) 
    		ON DELETE CASCADE
    )

    where `RestaurantId` is the primary key and FK from `Restaurants` table.

    And

    CREATE TABLE [dbo].[Restaurants]
    (
    	[Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    	[Name] NVARCHAR(50) NOT NULL,  
    
    	CONSTRAINT [FK_Restaurants_TCategories] 
    		FOREIGN KEY ([IdCategory]) REFERENCES [Categories]([Id]) 
    )


    This is my model:

    public class Restaurant
    {
    	[Key]
    	public int Id { get; set; }
    
    	[Required, StringLength(50)]
    	public string Name { get; set; }
    
    	public Address Address { get; set; }
    }
    	
    	
    public class Address
    {
    	[Required, StringLength(150)]
    	public string Street { get; set; }
    
    	[StringLength(8)]
    	public string Number { get; set; }
    
    	[Required, StringLength(10)]
    	public string Zip { get; set; }
    
    	[Required, StringLength(50)]
    	public string Town { get; set; }
    
    	[StringLength(150)]
    	public string Site { get; set; }
    }


    I defined my owned property like this in `OnModelCreating`:

    modelBuilder.Entity<Restaurant>()
    	.OwnsOne(p => p.Address)
    	.ToTable("Addresses");

    And I save like this:

    await _dbContext.Set<Restaurant>()
    		.AddAsync(restaurant, cancellationToken);
    _dbContext.SaveChangesAsync();	

    I tested a synchronous version as well.


    What I am looking for


    What should I change in order to make EF understand `RestaurantId` should get the newly created Id from `Restaurant` table before inserting the `Address`?


    Thank you!

    Monday, September 30, 2019 8:53 AM

All replies

  • Hi,

    Maybe you can try to add Restaurant manually before "insert". Here is a related thread may help you.

    https://stackoverflow.com/a/45760626/8335151

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 4, 2019 6:14 AM
    Moderator
  • Yes this work.

    But if this is the solution, I don't need an owned property and my project is specifically dedicated to test this new feature.

    Owned properties work fine with GET, DELETE, UPDATE but not INSERT. This lower significantivly their interrest.


    Thank you!

    Friday, October 4, 2019 7:16 PM
  • Hi,

    If so, maybe you can try to submit an issue at github aspnet/EntityFrameworkCore.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 7, 2019 2:43 AM
    Moderator