npgsql / EntityFramework6.Npgsql

Entity Framework 6 provider for PostgreSQL
PostgreSQL License
66 stars 53 forks source link

PostgreException when trying to run a seed in a CodeFirst database #97

Closed xecollons closed 6 years ago

xecollons commented 6 years ago

Hello,

These days we're trying to upgrade our project to Npgsql 4.0.0 from 3.2.7, but we found that we can't run our seed when we do an "update database".

Snippet of code:

    public class InterestLocation : IEntity
    {
        public long Id { get; set; }
        public DateTime LastModDate { get; set; }
        public string LastModUser { get; set; }
        public bool Deleted { get; set; }
        public State State { get; set; }
    }

Mapping:

    internal class InterestLocationMap : EntityTypeConfiguration<InterestLocation> 
    {
         public InterestLocationMap()
        {
            ToTable("localizacion_interes");
            HasKey(ua => ua.Id);

            Property(ua => ua.Id).HasColumnName("id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            Property(ua => ua.Deleted).HasColumnName("deleted");

            Property(ua => ua.LastModDate).HasColumnName("last_mod_user");

            Property(ua => ua.LastModUser)
                .HasColumnName("last_mod_user")
                .HasMaxLength(20);

            Ignore(ua => ua.State);
        }
    }

And the seed:

            //...
            var loc = new InterestLocation();
            loc.LastModDate = DateTime.Now;
            loc.LastModUser = _usuarioUltimaModificacion;
            loc.Deleted = false;

            _context.Set<InterestLocation>().Add(loc);
            //...
            _context.SaveChanges();

And it gives a 42703 error, "«p_0» column doesn't exists". The innerexception gives us this insert statement: INSERT INTO "public"."interest_location"("last_mod_date","last_mod_user","deleted") VALUES (@p_0,@p_1,@p_2) RETURNING "id" with an empty list of "InputParameters". Maybe there's something that it's stopping the insert statement generator to get parameters?

I'm using Npgsql 4.0.0-rc1 and EntityFramework6.Npgsql 3.1.1. It worked in Npgsql 3.2.7.

Thanks.

roji commented 6 years ago

Thanks for raising this - it was indeed a bug in Npgsql 4.0 (https://github.com/npgsql/npgsql/issues/1957).

I've just pushed a fix, can you please try the latest version of Npgsql 4.0 from the unstable feed and report? I have a basic EF6 test working but it would be good to have some more testing.

xecollons commented 6 years ago

Seed method seems to work well now, thanks.

roji commented 6 years ago

Thanks for testing!