本文介绍了在ef core 2.0中具有复杂类型的列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想使用流利的api更改属性的列类型,但是我遇到了错误I want to change column type of property using fluent api, but i have an error 表达式'x => x.NestedProp.Prop1'不是有效的属性表达式。表达式应该表示属性访问:'t => t.MyProperty'。 请,我不想使用DataAnnotations这是我的代码(类):public class Class1 { public int Id { get; set; } public NestedProp NestedProp { get; set; } = new NestedProp();}public class NestedProp { public decimal Prop1 { get; set; }}和 OnModelCreating:protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity<Class1>(e => { e.OwnsOne(t => t.NestedProp); e.Property(p => p.NestedProp.Prop1).HasColumnType("NUMERIC(38, 16)"); });}推荐答案请尝试以下操作并注意类 NestedProp 中的 Id 属性。 EF(核心)在每个模型中都需要一个主键,否则它将不起作用。Please give the following a try and pay attention to the Id property in your class NestedProp. EF (Core) needs in every model a primary key, otherwise it will not work. 模型public class Class1{ public int Id { get; set; } public virtual NestedProp NestedProp { get; set; } = new NestedProp();}public class NestedProp{ public int Id { get; set; } public decimal Prop1 { get; set; }} OnModelCreatingprotected override void OnModelCreating(ModelBuilder modelBuilder){ var class1Entity = modelBuilder.Entity<Class1>(); class1Entity.HasOne(p => p.NestedProp); var nestedPropEntity = modelBuilder.Entity<NestedProp>(); nestedPropEntity.Property(p => p.Prop1) .HasColumnType("NUMERIC(38, 16)"); // maybe decimal instead of numeric?}在这里您会发现更多解释。 必须为每个模型定义配置。Here you will find some more explanation.You have to define the configuration for each model.我建议使用 IEntityTypeConfiguration< T> 而不是在 OnModelCreating 中配置所有内容。I recommend to use IEntityTypeConfiguration<T> instead of configure everything in OnModelCreating.在EF Core中使用Fluent API的不错介绍会发现此处或此处。A nice introduction for using the Fluent API in EF Core would you find here or here. 编辑:上面的解决方案将创建两个表,因为它实现了两个自己的数据类型。不是所问问题的复杂类型。因此,我也将为该解决方案提供建议。此拥有的类型映射可通过 entity.OwnsOne(...)方法实现。也可以像本 MS文档文章。在此文章中,您会找到The Solution above will create two tables, because it implements two own data types. Not the in question asked complex type in on column. Therefore I will give advise for this solution too. This owned type mapping is realizable via entity.OwnsOne(...) method. It is also possible to split in like mentioned in this MS doc article. In this article you will find how to configure it explicitly too.下面是带有代码和流畅API的示例:Here the example with your code and fluent API: 型号public class Class1{ public int Id { get; set; } public virtual NestedProp NestedProp { get; set; } = new NestedProp();}public class NestedProp{ public decimal Prop1 { get; set; }} OnModelCreatingprotected override void OnModelCreating(ModelBuilder modelBuilder){ var class1Entity = modelBuilder.Entity<Class1>(); class1Entity.OwnsOne( class1 => class1.NestedProp, nestedProp => { nestedProp.Property(p => p.NestedProp) .HasColumnType("NUMERIC(38, 16)") .HasColumnName("NestedPropValue"); // here you could add a custom name like I did or remove it and you get a generated one });} 这篇关于在ef core 2.0中具有复杂类型的列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 05-26 21:01