Problem with SaveChangesAsync

Oct 2, 2015 at 2:05 PM
Edited Oct 2, 2015 at 2:05 PM
I am having a problem getting the repository InsertOrUpdateGraph() method to work with SaveChangesAsync. Here is my test application:
public class User {
  public int UserId { get; set; }
  public string Email { get; set; }

  public virtual UserDetails Details { get; set; }
}

public class UserDetails {
  public int UserId { get; set; }
  public string Name { get; set; }
  public int Age { get; set; }
}
I then map them as follows:
public class UserMap : EntityTypeConfiguration<User>
{
  public UserMap() {
    ToTable("app_User");

    HasKey(u => u.UserId);

    Property(u => u.UserId)
      .IsRequired()
      .HasColumnName("UserId")
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

   Property(u => u.Email)
      .IsRequired()
      .HasMaxLength(255)
      .HasColumnName("UserId")

    HasRequired(u => u.Details)
        .WithRequiredPrincipal()
        .WillCascadeOnDelete();
  }
}

public class UserDetailsMap : EntityTypeConfiguration<UserDetails>
{
  public UserDetailsMap() {
    ToTable("app_UserDetails");
    
    HasKey(d => d.UserId);
    
    Property(d => d.UserId)
      .IsRequired()
      .HasColumnName("UserId");

   Property(u => u.Name)
      .IsRequired()
      .HasMaxLength(50)
      .HasColumnName("Name")

   Property(u => u.Age)
      .IsRequired()
      .HasColumnName("Age")
  }
}
If I then try and insert a new User...
var user = new User
{
        Email = "booha@somedomain",
    Details = new UserDetails
    {
            Name = "Boo Ha",
            Age = 99,
        ObjectState = ObjectState.Added
    },
    ObjectState = ObjectState.Added
};

_userService.InsertOrUpdateGraph(user);
await _unitOfWork.SaveChangesAsync();
I get the following exception...
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_app_UserDetails_app_User". The conflict occurred in database "TestDb", table "dbo.app_User", column 'UserId'.
The statement has been terminated.
But if I change the call to non-asynchronous, it works correctly. Interestingly if I change the lines
_userService.InsertOrUpdateGraph(user);
await _unitOfWork.SaveChangesAsync();
to
using (var ctx = new AppDataContext()) {
    ctx.Users.Add(user)
    ctx.SaveChangesAsync();
}
The insert runs without exception too.

Am I doing something wrong? Has anyone else experienced this problem?

Thanks.
Oct 2, 2015 at 2:24 PM
I have added a simple Windows console application to GitGub that seems to demonstrate the problem - see https://github.com/Neilski/URF-TestApp
Oct 11, 2015 at 11:51 PM
Edited Oct 11, 2015 at 11:51 PM
hello
I need your help if you please
my question is:
Did you face a problem with navigation properties(they don't load/always empty) when you are using URF v3.3.5?
I need to know if the problem is from my side or it's from the URF v3.3.5.
Thank you for your time.
Oct 12, 2015 at 6:48 AM
No, I did not have a problem with loading navigation properties. As I understand it you have two basic options (there might be other options so please check):
  1. Use the appropriate .Include() statement(s) when building the query if you are referencing the default URF data context, or...
  2. Re-enable LazyLoading in your DataContext constructor:
        public MyAppDataContext()
            : base("Name=MyAppConnectionString")
        {
            // Default URF configuration
            // Configuration.LazyLoadingEnabled = false;
            // Configuration.ProxyCreationEnabled = false;

            // Re-Enable
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = true;
        }
Hope that helps.
Oct 14, 2015 at 12:53 AM
Thank you a lot :)