EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

Update with custom type of postgresql throw an error

Open Ilya-by opened this issue 5 years ago • 3 comments

Description

Has custom type for enum in postgresql . Try to perform update this field and receive an exception

Exception

42804: column "Status" is of type customenumtype but expression is of type integer

Exception message:
Stack trace:
Npgsql.PostgresException (0x80004005): 42804: column "Status" is of type customenumtype but expression is of type integer
   at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at Z.EntityFramework.Extensions.BatchUpdate.<>c.(DbCommand , Nullable`1 )
   at Z.EntityFramework.Extensions.BatchUpdate.Execute[T](IQueryable`1 query, Expression`1 updateFactory)
   at BatchUpdateExtensions.UpdateFromQuery[T](IQueryable`1 query, Expression`1 updateFactory, Action`1 batchUpdateBuilder)
   at Z.EntityFramework.Plus.BatchUpdateExtensions.Update[T](IQueryable`1 query, Expression`1 updateFactory, Action`1 batchUpdateBuilder)
   at Z.EntityFramework.Plus.BatchUpdateExtensions.<>c__DisplayClass2_0`1.<UpdateAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---

### Further technical details
- EF version: 5.0.0
- EF Extensions version: Npgsql.EntityFrameworkCore.PostgreSQL 5.0.0
- Database Provider: Z.EntityFramework.Plus.EFCore 5.1.2

Ilya-by avatar Nov 24 '20 15:11 Ilya-by

Hello @Ilya-by ,

Did you specify in your ModelBuilder that you had enum type?

Something like this:

protected override void OnModelCreating(ModelBuilder builder)
{
	builder.HasPostgresEnum<TestEnum>();
}

Here is a full working example:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EFCore50.PostgreSQL
{
	public class Request_Enum
	{
		public static void Execute()
		{
			bool createAndDeleteBD = true;

			if (createAndDeleteBD)
			{
				// Create BD 
				using (var context = new EntityContext())
				{
					My.DeleteBD(context);
					context.Database.EnsureCreated();
				}
			}

			// CLEAN  
			using (var context = new EntityContext())
			{
				context.EntitySimples.RemoveRange(context.EntitySimples);

				context.SaveChanges();
			}


			// SEED  
			using (var context = new EntityContext())
			{
				context.EntitySimples.Add(new EntitySimple { ColumnInt = 1 });

				context.EntitySimples.Add(new EntitySimple { ColumnInt = 2, TestEnum = TestEnum.Test2 });

				context.EntitySimples.Add(new EntitySimple { ColumnInt = 3 });

				context.SaveChanges();
			}



			// TEST  
			using (var context = new EntityContext())
			{
				List<EntitySimple> list = new List<EntitySimple>();
				list.Add(new EntitySimple { ColumnInt = 10 });
				list.Add(new EntitySimple { ColumnInt = 11 , TestEnum = TestEnum.Test2});
				list.Add(new EntitySimple { ColumnInt = 12 });

				context.BulkInsert(list);
			}
			// TEST  
			using (var context = new EntityContext())
			{
				var test = context.EntitySimples.ToList();
			}
			// TEST  
			using (var context = new EntityContext())
			{
				context.EntitySimples.UpdateFromQuery(x => new EntitySimple() { TestEnum = TestEnum.Test2 });
			}
			// TEST  
			using (var context = new EntityContext())
			{
				var test = context.EntitySimples.ToList();
			}
		}

		public class EntityContext : DbContext
		{
			public EntityContext()
			{
			}

			public DbSet<EntitySimple> EntitySimples { get; set; }
			protected override void OnModelCreating(ModelBuilder builder)
			{
				builder.HasPostgresEnum<TestEnum>();
			}

			protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
			{
				optionsBuilder.UseNpgsql(My.Connection);
			}
		}

		public class EntitySimple
		{
			public int ID { get; set; }
			public int ColumnInt { get; set; }
			public String ColumnString { get; set; }
			public TestEnum TestEnum { get; set; }
		}
		public enum TestEnum
		{ 
			Test1,
			Test2
		}
	}
}

Let me know if that's working or you still have a problem.

Best Regards,

Jon


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

JonathanMagnan avatar Nov 25 '20 22:11 JonathanMagnan

Hello @Ilya-by ,

Since our last conversation, we haven't heard from you.

Did you get the time to work on the issue?

Don't hesitate to contact us for further assistance.

Best regards,

Jon

JonathanMagnan avatar Nov 27 '20 13:11 JonathanMagnan

Hello again @Ilya-by

A simple reminder that we are here to assist you!

Feel free to contact us once you get time to work on the issue.

Best regards,

Jon

JonathanMagnan avatar Dec 04 '20 14:12 JonathanMagnan