This is a migrated thread and some comments may be shown as answers.

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

5 Answers 55 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 15 Jul 2016, 07:04 PM

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...

5 Answers, 1 is accepted

Sort by
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 15 Jul 2016, 07:27 PM

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();
                                }

0
Ralph Waldenmaier
Telerik team
answered on 18 Jul 2016, 02:16 PM
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.
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 18 Jul 2016, 02:19 PM

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

0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 14 Dec 2016, 09:47 PM

I've been dreading coming back to this, but I finally picked up the Git issue to try it... I'm already mapping like this AFAIK, doesn't work still! :/

It's not a mapping problem is it, it's the insane converting 0 to null issue?

 

public MappingConfiguration<MppStudentPg> GetMppStudentPgMappingConfiguration()
{
    MappingConfiguration<MppStudentPg> configuration = new MappingConfiguration<MppStudentPg>();
    configuration.MapType(x => new { }).WithDataAccessKind(DataAccessKind.ReadWrite).ToTable("mpp_student_pg");
 
    configuration.HasProperty(x => x.UserID).IsIdentity(KeyGenerator.Guid).HasFieldName("userID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("userid").IsNotNullable().HasColumnType("uniqueidentifier").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.StartDate).HasFieldName("startDate").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("startdate").IsNullable().HasColumnType("datetime");
    configuration.HasProperty(x => x.AdvancementDate).HasFieldName("advancementDate").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("advancementDate").IsNullable().HasColumnType("datetime");
    configuration.HasProperty(x => x.Pgy).HasFieldName("pgy").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("pgy").IsNullable().HasColumnType("tinyint").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.Chiefresident).HasFieldName("chiefresident").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("chiefresident").IsNullable().HasColumnType("bit").HasPrecision(0).HasScale(0).HasDefaultValue();
    configuration.HasProperty(x => x.ClinicalFellow).HasFieldName("clinicalFellow").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("clinicalFellow").IsNullable().HasColumnType("bit").HasPrecision(0).HasScale(0).HasDefaultValue();
    configuration.HasProperty(x => x.ProgramID).HasFieldName("programID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("programID").IsNullable().HasColumnType("smallint").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.LastUpdated).HasFieldName("lastUpdated").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("lastupdated").IsNullable().HasColumnType("datetime");
    configuration.HasProperty(x => x.CIP).HasFieldName("cIP").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("CIP").IsNullable().HasColumnType("bit").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.BCT).HasFieldName("bCT").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("BCT").IsNullable().HasColumnType("bit").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.ImgAvpEndDate).HasFieldName("imgAvpEndDate").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("AvpEndDate").IsNullable().HasColumnType("datetime").HasDefaultValue();
 
    //Association
    configuration.HasAssociation(x => x.CommonProgram).HasFieldName("commonProgram").WithOpposite(x => x.MppStudentPgs).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppStudent).HasFieldName("mppStudent").WithOpposite(x => x.MppStudentPg).ToColumn("userid").IsManaged().IsRequired().WithDataAccessKind(DataAccessKind.ReadWrite);
 
    return configuration;
}

0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 14 Dec 2016, 09:48 PM

Reverse object

public MappingConfiguration<CommonProgram> GetCommonProgramMappingConfiguration()
{
    //Config
    MappingConfiguration<CommonProgram> configuration = new MappingConfiguration<CommonProgram>();
    configuration.MapType(x => new { }).WithDataAccessKind(DataAccessKind.ReadWrite).ToTable("common_program");
 
    //Mapping
    configuration.HasProperty(x => x.ProgramID).IsIdentity().HasFieldName("programID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("programID").IsNotNullable().HasColumnType("smallint").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.ProgramName).HasFieldName("programName").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("programName").IsNotNullable().HasColumnType("nvarchar").HasLength(100);
    configuration.HasProperty(x => x.Abbr).HasFieldName("abbr").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("abbr").IsNullable().HasColumnType("nvarchar").HasLength(8);
    configuration.HasProperty(x => x.Shortcode).HasFieldName("shortcode").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("shortcode").IsNullable().HasColumnType("nchar").HasLength(2);
    configuration.HasProperty(x => x.ParentProgramID).HasFieldName("parentProgramID").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("parentProgram").IsNullable().HasColumnType("smallint").HasPrecision(0).HasScale(0);
    configuration.HasProperty(x => x.BCT).HasFieldName("bCT").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("BCT").IsNotNullable().HasColumnType("bit").HasPrecision(0).HasScale(0).HasDefaultValue();
    configuration.HasProperty(x => x.IsTest).HasFieldName("isTest").WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn("isTest").IsNotNullable().HasColumnType("bit").HasPrecision(0).HasScale(0).HasDefaultValue();
 
    //Associations
    configuration.HasAssociation(x => x.ParentProgram).HasFieldName("parentProgram").WithOpposite(x => x.ChildPrograms).ToColumn("parentProgram").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppFaculty).HasFieldName("mppFaculty1").WithOpposite(x => x.CommonPrograms).IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite).MapJoinTable("mpp_faculty_program", (x, y) => new { programID = x.ProgramID, userid = y.UserID }).CreatePrimaryKeyFromForeignKeys();
    configuration.HasAssociation(x => x.ChildPrograms).HasFieldName("childPrograms").WithOpposite(x => x.ParentProgram).ToColumn("parentProgram").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.CpsProgramDesigns).HasFieldName("cpsProgramDesigns1").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppStudentPgs).HasFieldName("mppStudentPgs").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppTrainingHistories).HasFieldName("mppTrainingHistories").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MpAccountPreps).HasFieldName("mpAccountPreps").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppAdministratorPgPrograms).HasFieldName("mppAdministratorPgPrograms").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.MppUserNetworks).HasFieldName("mppUserNetworks").WithOpposite(x => x.CommonProgram).ToColumn("programID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
    configuration.HasAssociation(x => x.StudentaffairsMentors).HasFieldName("studentaffairsMentors").WithOpposite(x => x.CommonProgram).ToColumn("residencyProgramID").IsManaged().WithDataAccessKind(DataAccessKind.ReadWrite);
 
    return configuration;
}
Tags
General Discussions
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Ralph Waldenmaier
Telerik team
Share this question
or