简体   繁体   English

如何在EF Core中为属性手动配置数据库类型?

[英]How to manually configure the database type for a property in EF Core?

I have a class: 我有一堂课:

class Order
{
    public OrderId Id {get;set;}
}

struct OrderId
{
    private int readonly id;

    public OrderId(int id)
    {
        this.id = id;
    }
}

For some reason, I don't want to use a primitive type for the property. 由于某些原因,我不想为该属性使用原始类型。 When I'm trying to save object of this class to Sql Server through Entity Framework Core , I get an error: 当我尝试通过Entity Framework Core将此类的对象保存到Sql Server ,出现错误:

System.InvalidOperationException: 'The property 'Order.Id' is of type 'OrderId' which is not supported by current database provider. System.InvalidOperationException:'属性'Order.Id'的类型为'OrderId',当前数据库提供程序不支持。 Either change the property CLR type or manually configure the database type for it.' 要么更改属性CLR类型,要么为其手动配置数据库类型。”

Question: Is it possible to configure mapping in such a case and how to do it? 问题:在这种情况下是否可以配置映射以及如何配置?

EF Core(最新版本2.0)目前不支持此功能。有关跟踪问题和可能的解决方法,请参阅https://github.com/aspnet/EntityFramework/issues/9194

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core - 如何根据使用的数据库提供程序配置列类型 - EF Core - How to Configure Column Type depending on the Database Provider used 如何在 EF Core 5 中为自定义 SQL 配置导航属性 - How to configure navigation property for custom SQL in EF Core 5 如何在 ef core 6 中配置引用另一个实体的拥有类型 - How to configure Owned type referencing another entity in ef core 6 EF Core 3:配置导航属性的支持字段 - EF Core 3: Configure backing field of navigation property 如何使用 EF Core 将 map MySQL Geometry 类型属性转换为 .NET Core Geometry 类型属性? - How to map MySQL Geometry type property with EF Core to .NET Core Geometry type property? 如何在 EF Core 6.0 中使用“枚举”作为属性类型 - How to use `enum` as a property type in EF Core 6.0 如何使用 EF Core 处理更改 SQL 服务器数据库列类型 - How to handle changing SQL Server database column type with EF Core EF Core 6 导航属性未在数据库上更新 - EF Core 6 Navigation Property not updated on database 如何使用 EF Core 5 仅在一侧(在集合端)配置与导航属性的一对多关系? - How to configure one-to-many relationship with navigation property only on one side (on collection side) with EF Core 5? 如何在EF Core 2.1中配置MaxIdentifierLength常量? - How to configure MaxIdentifierLength constant in EF Core 2.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM