This is just a comment, but I can not write it in the comments ...
The first time I see an exception
It is not possible to determine the actual ordering for dependent operations. Dependencies may exist due to foreign key constraints, requirements model, or store values.
so I tried to reproduce it.
So I implemented the missing classes and context
public class Entity {} public class Tenant {} public class AbpUser<T1, T2> {} public Context(DbConnection connection) : base(connection, false) { } public DbSet<Advertisement> Advertisements { get; set; } public DbSet<User> Users { get; set; } public DbSet<AdImage> AdImages { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<User>() .HasMany(u => u.Advertisements) .WithRequired(x => x.User); modelBuilder.Entity<Advertisement>() .HasMany(a => a.AdImages) .WithRequired(x => x.Advertisement); modelBuilder.Entity<AdImage>() .HasRequired(x => x.Advertisement); base.OnModelCreating(modelBuilder); }
These are the DDL statements created by EF during automatic migration.
ExecuteNonQuery========== CREATE TABLE [AdImages] ( [Id] int not null identity(1,1) , [Image] text null , [Advertisement_Id] int not null ); ALTER TABLE [AdImages] ADD CONSTRAINT [PK_AdImages_87d4bad2] PRIMARY KEY ([Id]) ExecuteNonQuery========== CREATE TABLE [Advertisements] ( [Id] int not null identity(1,1) , [Title] text null , [Message] text null , [User_UserId] int not null ); ALTER TABLE [Advertisements] ADD CONSTRAINT [PK_Advertisements_5d578c9a] PRIMARY KEY ([Id]) ExecuteNonQuery========== CREATE TABLE [Users] ( [UserId] int not null identity(1,1) , [AccessToken] text null ); ALTER TABLE [Users] ADD CONSTRAINT [PK_Users_5d578c9a] PRIMARY KEY ([UserId]) ExecuteNonQuery========== CREATE INDEX [IX_Advertisement_Id] ON [AdImages] ([Advertisement_Id]) ExecuteNonQuery========== CREATE INDEX [IX_User_UserId] ON [Advertisements] ([User_UserId]) ExecuteNonQuery========== ALTER TABLE [AdImages] ADD CONSTRAINT [FK_AdImages_Advertisements_Advertisement_Id] FOREIGN KEY ([Advertisement_Id]) REFERENCES [Advertisements] ([Id]) ExecuteNonQuery========== ALTER TABLE [Advertisements] ADD CONSTRAINT [FK_Advertisements_Users_User_UserId] FOREIGN KEY ([User_UserId]) REFERENCES [Users] ([UserId])
So, actually everything is as expected.
And here is the test I tried
public static void Run(DbConnection connection) { var ad = new Advertisement { AdImages = new List<AdImage> { new AdImage {Image = "MyImage"} }, Message = "MyMessage", Title = "MyTitle", User = new User() }; using (Context context = new Context(connection)) { context.Advertisements.Add(ad); context.SaveChanges(); } }
It triggered these queries in the database
ExecuteDbDataReader========== insert into [Users]([AccessToken]) values (null); select [UserId] from [Users] where [UserId] = @@identity ExecuteDbDataReader========== insert into [Advertisements]([Title], [Message], [User_UserId]) values (@p0, @p1, @p2); select [Id] from [Advertisements] where [Id] = @@identity @p0 = MyTitle @p1 = MyMessage @p2 = 1 ExecuteDbDataReader========== insert into [AdImages]([Image], [Advertisement_Id]) values (@p0, @p1); select [Id] from [AdImages] where [Id] = @@identity @p0 = MyImage @p1 = 1
Your model is just perfect :)
So the problem is elsewhere. It can be in code around, i.e. - Where do model and user come from? The same context that you use in Upsert or in another context?
- What are you doing at Upsert? Can you link some links (from model to a new object)?
- other missing classes (those that I left empty)?