c# - Npgsql/EF 6 - json 列

标签 c# asp.net-mvc entity-framework npgsql entity-framework-migrations

我正在尝试使用 JSON 列创建迁移。这是我尝试过的:

    [Column(TypeName = "Jsonb")]
    public string Data { get; set; }

    [Column(TypeName = "Json")]
    public string Data { get; set; }

    modelBuilder.Entity<Member>().Property(p => p.Data).HasColumnType("Json");

    modelBuilder.Entity<Member>().Property(p => p.Data).HasColumnType("Jsonb");

什么都不起作用,这里是异常(exception):

System.InvalidOperationException: Sequence contains no matching element at System.Linq.Enumerable.Single[TSource](IEnumerable1 source, Func2 predicate) at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.<>c__DisplayClass4.b__3(Tuple2 pm) at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable1 ts, Action1 action) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride) at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride) at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection1 entitySets, DbProviderManifest providerManifest) at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() at System.Data.Entity.Internal.Linq.InternalSet1.get_Local() at System.Data.Entity.DbSet`1.get_Local() at System.Data.Entity.DbModelBuilderExtensions.RegisterUserAccountChildTablesForDelete[TKey,TAccount,TUserClaim,TLinkedAccount,TLinkedAccountClaim,TPasswordResetSecret,TTwoFactorAuthToken,TUserCertificate](DbContext ctx) in c:\ballen\github\brockallen\BrockAllen.MembershipReboot\src\BrockAllen.MembershipReboot.Ef\DbModelBuilderExtensions.cs:line 26

这是我的配置:

<package id="EntityFramework" version="6.1.1" targetFramework="net452" />
<package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net452" />
<package id="Npgsql" version="3.1.6" targetFramework="net452" />

最佳答案

因此,这是完全可行的,但需要修改生成的迁移,而不是注释您的模型或使用流畅的配置。在生成的迁移中,更改 data 列的声明以使用 storeTypedefaultValueSql 参数:

data = c.String(nullable: false, storeType: "jsonb", defaultValueSql: "'{}'::jsonb")

我可以确认这适用于带有 EntityFramework6.Npgsql 3.1.1 的 Npgsql 3.1.7,并且将毫无问题地保存和加载适用的实体。无法保证早期版本。

关于c# - Npgsql/EF 6 - json 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38417082/

相关文章:

c# - 使用 Math.Round 转换特定对象类型的所有属性值?

c# - 如何更改默认的 WCF 服务绑定(bind)?

entity-framework - Entity Framework 核心 - FromSql 错误 :

entity-framework - 为什么DbContext每次都在dispose?

c# - 无法加载文件或程序集...系统找不到指定的文件 C#

asp.net - 您可以将模型属性从 ASP.NET 模型传递到 ReactJS.NET 组件吗?

asp.net-mvc - 在 MVC 5 应用程序中使用 autofac 注入(inject) SignalR Hub 的依赖项

c# - ASP.NET MVC5 在 ListView 中获取所有注册的 AspNetUsers

c# - LINQ BeginTransaction 可以使用新的 Context 吗?

c# - 在 Entity Framework 5 Code First 中覆盖 SaveChanges 以复制旧遗留库的行为