Relax and turn of constraint!!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi all,

Having an issue with the Db again, right we have completed all nessercary feilds and mirrored our extisting database for the purposes of replication through 2 systems.

I had to recreate fields that completely mirrored the ones we have and I can't make any changes to this... I need to in the code. I have began updating the existing entity. By adding all nesercary fields into the core, data etc.

On launching the application and viewing a product I'm hit with this error,

The 'CCCPack' property on 'Product' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'.
In Visual Studio - NopObjectContext in bold is debugging error

Constraint Exception

//database call
                    var reader = cmd.ExecuteReader();
                    //return reader.DataReaderToObjectList<TEntity>();
                    var result = context.Translate<TEntity>(reader).ToList();
                    for (int i = 0; i < result.Count; i++)
                        result[i] = AttachEntityToContext(result[i]);
                    //close up the reader, we're done saving results
                    reader.Close();
                    return result;

I read else where on the forum to add a constraint in the db to allow to make a default of 1 etc but we can't do this I need to work it in the code due to the mirror on our existing db etc.

I have added the following in the entity but Im still hit with the same error is there a way I can turn of the constraints or relax them when uploading through the dataset????

NopCore/Domain/Product.cs
public virtual Int32 CCCPack { get; set; }


NopData/Mapping/ProductMap.cs

this.Property(p => p.CCCPack).IsOptional();


NopWeb/Models/Catalog/Product.cs

 public Int32 CCCPack { get; set; }


NopWeb/Controller/CatalogController

model.CCCPack = product.CCCPack;


All help would be highly regarded.

Richard.
12 years ago
The problem is your field in the DB is set to allow nulls but your code says there should be no nulls.

Change the line in your Core file to this:

public virtual int? CCCPack { get; set; }


The ? makes it nullable.  Right now when you're reading in the record, you've got a null value in that field and the system doesn't know how to make that an int.  

One side effect of this change will be anywhere you're referencing CCCPack, you'll need to check CCCPack.HasValue and if you want the number use CCCPack.Value.
12 years ago
Yeah thanks Andy.

Made the rearrangements in the core and the controller thank you.

Thanks again.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.