neisbut / Npgsql.Bulk

Helper for performing COPY (bulk insert and update) operation easily, using Entity Framework + Npgsql.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jsonb Property error in .net core and entity framework core 3.0

MinhMit opened this issue · comments

Hi,

First, sorry my Eng's not good.

Second, I have a project using entity framework core 3.0 and running SQL Server 2016 with some properties of entities are json.

Example:

public class Role
{
        public Role()
        {
            Priority = 1;
        }
        public string Name { get; set; }
        public string Description { get; set; }
        public int Priority { get; set; }
        public Object Property { get; set; }
}

and DbContext:

 builder.Entity<Role>(entity =>
            {
                entity.Property(role => role.Property).HasConversion(
                    role => JsonConvert.SerializeObject(role, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
                    role => JsonConvert.DeserializeObject<Object>(role, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })
                    );
                entity.HasIndex(role => new { role.Id, role.Name }).Include<Role>(role => new { role.Description, role.Property }).IsUnique();
            });

And when i execute bulk insert have error: Can't write CLR type Newtonsoft.Json.Linq.JObject with handler type TextHandler.

I want model and dbcontext can switch between SQL Server 2016 and postgresql

Update: when i use,

dbContext.Roles.AddRange(entitites); 
dbContext.SaveChanges();

It's work.

Thanks for your support

Hi, perhaps using

        private YourObjectType _property;
        [NotMapped]
        public YourObjectType Property
        {
            get
            {
                return _property;
            }
            set
            {
                _property = value;
                _propertySerialized = JsonConvert.SerializeObject(role, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
        }

        private string _propertySerialized;
        public string PropertySerialized
        {
            get
            {
                return _propertySerialized;
            }
            set
            {
                _propertySerialized = value;
                _property = JsonConvert.DeserializeObject<YourObjectType>(value, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
        }

instead, will do the work?

Hi, perhaps using

        private YourObjectType _property;
        [NotMapped]
        public YourObjectType Property
        {
            get
            {
                return _property;
            }
            set
            {
                _property = value;
                _propertySerialized = JsonConvert.SerializeObject(role, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
        }

        private string _propertySerialized;
        public string PropertySerialized
        {
            get
            {
                return _propertySerialized;
            }
            set
            {
                _propertySerialized = value;
                _property = JsonConvert.DeserializeObject<YourObjectType>(value, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
            }
        }

instead, will do the work?

Thanks for your support.

But my project has been run at production environment with SQL Server version. I can't modify entities or property name of entities.

Hi @MinhMit , sorry for such a late answer. Problem seems to happen because before HasConversion was not supported. Now it should work, please try the latest release.

@pacoferre, thanks for your support. Very appreciated!

Hi @MinhMit , sorry for such a late answer. Problem seems to happen because before HasConversion was not supported. Now it should work, please try the latest release.

Thanks for your support!

Closed due to inactivity