Results 1 to 3 of 3

Thread: [RESOLVED] Entity Framework: saving empty string as dbnull?

  1. #1
    Frenzied Member tr333's Avatar
    Join Date
    Nov 04
    Location
    /dev/st0
    Posts
    1,426

    Resolved [RESOLVED] Entity Framework: saving empty string as dbnull?

    I'm using EntityFramework 4.3 Code First. How can I save an empty string value as NULL in the database? EF is throwing a System.Data.Entity.Validation.DbEntityValidationException whenever I attempt to save an entity with a null string value for one of it's properties.
    Don't forget about rep points if you think a post has benefited you in any way.
    Just another Perl hacker,

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,821

    Re: Entity Framework: saving empty string as dbnull?

    I haven't used EF 4.3 or Code First but I can't imagine that it's that much different in this fundamental regard. Is the database column generated to map to your string property nullable? If not then that would explain the exception. In that case you presumably need to decorate the property with an appropriate attribute to specify that it is nullable. I would have expected nullable to be the default though. That is supported by the existence of the Required attribute, which basically means not nullable.

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 04
    Location
    /dev/st0
    Posts
    1,426

    Re: Entity Framework: saving empty string as dbnull?

    Thanks. I had .IsRequired() set for that column in the EF mapping class. Don't know how I missed that one.
    Don't forget about rep points if you think a post has benefited you in any way.
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •