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

c# entity-framework-core

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.

What I did

My table Address 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 Restaurant 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]) 
)

I defined my 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();      

What I am looking for

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

I am using EF Core 3.

Update works fine, I just have a problem with creating an new restaurant/address

EDIT: 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; }
}

Edit2 : I tested a synchronous version as well

1
0
9/29/2019 12:41:20 PM

Accepted Answer

In fact this is probably a bug in EF 3.0 I tested with EF 3.1 (preview) and it is working fine

0
10/20/2019 2:31:41 PM

Popular Answer

In this case, you have a Class object which has a collection of addresses.

using (var context = new YourContext())
{
var model= new Restaurant{ Name = "McDonald's" };
model.addresses.Add(new addresses{ street="test",.... });
model.addresses.Add(new addresses{ street="test",.... });

context.Restaurant.Add(model);
context.SaveChanges();
}

this would solve your problem.

You can add in both your classes

public ICollection<YourEntity> YourEntity{ get; set; }

or you can use foreign keys.

    [ForeignKey("Restaurant")]
    public long RestaurantId { get; set; }
    public Restaurant Restaurant{ get; set; }

if you add this in your address entity you need to first create one resturant then add addresses separately



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