Saving entity with owned property

2019-08-22 01:55发布

问题:

Ok, this might be a dumb question, but I can't figure it out (tried searching but with no reasonable effect).

I have a model:

public class Freelancer
{
    public Address Address { get; set; }
    public Guid Id { get; set; }
}

public class Address
{
    public string Name1 { get; set; }
    public string Name2 { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }
}

which is configured as follows:

modelBuilder.Entity<Freelancer>().HasKey(x => x.Id);
modelBuilder.Entity<Freelancer>().OwnsOne(x => x.Address).Property(x => x.Name1).HasColumnName("AddressName1");
modelBuilder.Entity<Freelancer>().OwnsOne(x => x.Address).Property(x => x.Name2).HasColumnName("AddressName2");
modelBuilder.Entity<Freelancer>().OwnsOne(x => x.Address).Property(x => x.ZipCode).HasColumnName("AddressZipCode");
modelBuilder.Entity<Freelancer>().OwnsOne(x => x.Address).Property(x => x.City).HasColumnName("AddressCity");

and then I have a method that tries to create new Freelancer entity along with it's Address:

var address = new Address(command.Name1, command.Name2, command.ZipCode, command.City);
var freelancer = new Freelancer();
freelancer.Address = address;
_freelancerRepository.SaveOrUpdate(freelancer);
_freelancerRepository.SaveChanges();

Where SaveOrUpdate method basically does what dbContext.Set<Freelancer>().Add(...) is doing. And SaveChanges does exactly what dbContext.SaveChanges() is doing.

And now, SaveChanges throws an exception that's saying:

The entity of type 'Address' is sharing the table 'Freelancers' with entities of type 'Freelancer', but there is no entity of this type with the same key value that has been marked as 'Added'.

What do I do wrong?

PS.

I've also tried

modelBuilder.Entity<Freelancer>().OwnsOne(x => x.Address, a => { a.Property(...

implementation with exact same results.

Update: Heres my dbContext class:

public class DataContext : DbContext
{
    public DataContext(DbContextOptions<DataContext> options) : base(options)
    { }

    public DbSet<Freelancer> Freelancers { get; set; }
    public DbSet<Address> Addresses { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // configuration as above
    }
}

And, that's custom SaveOrUpdate implementation:

public void SaveOrUpdate(TDatabaseObject obj)
{
    if (_context.Set<TDatabaseObject>().Any(x => x.Id == obj.Id))
    {
        _context.Update(obj);
    }
    else
    {
        _context.Add(obj);
    }
}