13

I have the following model:

public class DeviceConfigurationModel
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Device Configuration Id")]
    public int DeviceConfigurationId { get; set; }

    [Required]
    [Display(Name = "Device Profile Name")]
    [StringLength(50)]
    public string ProfileName { get; set; }

    [StringLength(50)]
    public string SERV { get; set; }

    [StringLength(50)]
    public string IPAD { get; set; }

    public Nullable<int> PORT { get; set; }

    [Required]
    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }

    [Required]
    [Display(Name = "Date Created")]
    public DateTime DateCreated { get; set; }
}

which I seed through the package manager console with the command update-database and the following code in the configuration.cs for the migration:

    context.DeviceConfigurations.AddOrUpdate(
         d => new { d.ProfileName, d.IPAD, d.PORT, d.IsActive },
              new DeviceConfigurationModel { ProfileName = "FMG Default Gateway", IPAD = "77.86.28.50", PORT = (int?)90, IsActive = true  }
    );

However, whenever it tries to run this line of code I get the following error in the console:

The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.

does anyone know how to fix this problem, I have tried looking for answers but most of the solutions are to make it non-nullable and just accept a zero but I don't want to do this as I need to use a zero value for some of the fields

UPDATE

Having played with this further, I have narrowed this down to the list of things the update is run on: if I leave out the d.PORT from the line

d => new { d.ProfileName, d.IPAD, d.PORT, d.IsActive }

then the update works fine. Surely there must be a way to make the update also look at this field otherwise it seems that using mvc is pretty useless if it can't even handle a simple thing like a nullable int

Pete
  • 57,112
  • 28
  • 117
  • 166
  • I think this is at least an [explanation on why](http://stackoverflow.com/a/5067579/695586). And as for the "zero-solution", you could use -1 as well, or not? – Major Byte May 29 '13 at 17:14
  • Unfortunately the model has a lot more nullable ints than I'm showing and the data is being brought in from an external web service. Some of these fields need to use negative numbers too so I'm guessing that I'm going to have to use the addorupdate with these values so I'd prefer a proper solution – Pete May 30 '13 at 09:23
  • Does the table you are trying to generate here already exist in the DB? – Maciej Jun 04 '13 at 23:06
  • @Maciej The table gets generated when I run the update-database – Pete Jun 05 '13 at 07:49
  • @Pete OK. I wanted to eliminate the possibility that the table existed already in the DB and the script was not able to modify it. – Maciej Jun 05 '13 at 12:52

1 Answers1

2

Try setting the field to Optional explicitly using the Fluent API:

 public class YourContext : DbContext
    {
        public DbSet<DeviceConfigurationModel> DeviceConfigurations { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<DeviceConfigurationModel>().Property(x => x.PORT).IsOptional();
        }
    }

This should force the type to be nullable.

For some reason, declaring your property as int? instead of Nullable<int> will cause EF to generate your property as expected as well.

Maciej
  • 2,175
  • 1
  • 18
  • 29
  • Magic, this is exactly what I needed! It's a shame there isn't an attribute like the `required` one that you could add to the model that forces this to happen. Now I just need to remember to add the optional line for each nullable type I create. Thanks! – Pete Jun 05 '13 at 13:22
  • There is another thread that I found that indicates that you shouldn't have to do this (http://stackoverflow.com/questions/6011098/with-ef-4-1-code-first-how-can-i-create-a-nullable-column) but I'm glad it worked. – Maciej Jun 05 '13 at 13:24
  • Ah, I see my mistake now - I was using `Nullable` whereas I should have been using `int?`. The second one means that I don't have to add the extra line as you described – Pete Jun 05 '13 at 13:43
  • That is also interesting. isn't int? just compiler short form for Nullable? – Maciej Jun 05 '13 at 13:56
  • That's what I thought but somehow either EF or MVC treat them differently – Pete Jun 05 '13 at 13:57
  • 7
    This didn't work for me...My AddOrUpdate() is still throwing this exception. – Chris Mar 31 '14 at 21:00
  • 2
    I see no solution to this... IsOptional solves nothing – Dave Lawrence Mar 31 '16 at 13:08
  • 2
    Has anyone found a solution to this? Accepted answer does not seem to work anymore. Using EF 6.1 – TWilly Sep 28 '16 at 15:05