0->null on update problem, nothing seems to change it

4 posts, 0 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 15 Jul Link to this post

    So re: the ongoing issue where OA assumes 0 is the same as null...

    This is my model def

    configuration.HasProperty(x => x.ProgramID).HasFieldName("programID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("programID").IsNullable().HasColumnType("smallint").HasPrecision(0).HasScale(0);

     

    I have tried everything I can think of, but it's always NULL into the DB when the value is 0.  *note* this is an UPDATE not ADD.

    This is what I'm currently trying...just throwing the sink at it

    if (data.Postgrad.ProgramId.Value == 0)
    {
        user.MppStudentPg.ProgramID = -1;
        user.MppStudentPg.ProgramID = Convert.ToInt16(0);
    } else {
        user.MppStudentPg.ProgramID = Convert.ToInt16(data.Postgrad.ProgramId.Value);
    }

     

    This worked in a previous OA release, but now it's back to not working

    user.MppStudentPg.ProgramID = Convert.ToInt16(data.Postgrad.ProgramId.Value);

    This issue is the same chuck of code from ticket# 943882 but I can't seem to update that ticket anymore I think because I renewed and there's no support anymore :/

    Not sure what to do...

  2. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 15 Jul Link to this post

    Even trying this... so a seperate callback to the DB to grab the core (non-nav) object.  In debug I see it set to 0, the second I call .SaveChanges() it's back to null

    if (data.Postgrad.ProgramId.Value == 0)
                                    {
                                        var pgStudent = context.MppStudentPgs.FirstOrDefault(x => x.UserID == user.UserID);
                                        pgStudent.ProgramID = 1;
                                        pgStudent.ProgramID = 0;
                                        context.SaveChanges();
                                    }

  3. DevCraft banner
  4. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 18 Jul Link to this post

    Hello Steve,
    Thank you for providing your details. I was able to reproduce the reported behavior and also fix it using the latest version of the product. I was using the nuget packages in this case.
    The solution is to add a collection to the Program object and map it accordingly.

    Attached you can find a working example, but see the mapping before:
    MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
    productConfiguration.MapType(x => new
    {
        ID = x.ID,
        Price = x.Price,
        ProductName = x.ProductName,
        ProgramID = x.ProgramID
    }).ToTable("Products");
    productConfiguration.HasProperty(x => x.ID).IsIdentity(KeyGenerator.Autoinc);
    productConfiguration.HasProperty(x => x.ProgramID).HasFieldName("programID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("programID").IsNullable().HasColumnType("smallint").HasPrecision(0).HasScale(0);
    productConfiguration.HasAssociation(x => x.Program).ToColumn("programID").WithOpposite(x => x.Products);
    configurations.Add(productConfiguration);
     
     
    MappingConfiguration<Prog> progConfiguration = new MappingConfiguration<Prog>();
    progConfiguration.MapType(x => new
    {
        ProgID = x.ProgID,
        Name = x.Name
    }).ToTable("Program");
    progConfiguration.HasProperty(x => x.ProgID).IsIdentity(KeyGenerator.Default);
    progConfiguration.HasProperty(x => x.Name).IsNullable();
    configurations.Add(progConfiguration);

    See the .HasAssociation part. I am referencing the other part with .WithOpposite. That was the key to fix the example provided.

    I hope this solves things on your side as well.
    Do come back in case you need further assistance.

    Regards,
    Ralph Waldenmaier
    Telerik by Progress
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  5. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 18 Jul in reply to Ralph Waldenmaier Link to this post

    Will try, can't thank you enough for this much detail on a FORUM post over a ticket...

    Sidenote, will this logic ever ever be fixed, it makes just no logical sense and we're the only orm that suffers from this problem.  Keeps adding a new layer to DB design to make sure we're not using 0's anywhere

Back to Top