c# - 没有模型属性的 Entity Framework 映射结构/复杂类型

标签 c# entity-framework fluent entity-framework-6

我有一个“模型 struct”,它在内部是一个字符串,但在使用上有点类似于 GUID

public struct Token {

    private string _value;

    private Token(Guid uuid) {
        _value = Token.FromGuid(uuid);
    }

    public static Token FromGuid(Guid uuid) {
        // perform 'transformation'
        // stuff
        // return...
    }

    // other static methods to create a token...

}

如何使用 Entity Framework 6 Code First 映射此实体?我知道不支持结构,但是复杂类型似乎也不适用,因为不包含任何属性。

最佳答案

结构不是受支持的 EF 类型。参见 ef supported primitives

您需要改用复杂类型。

  • modelBuilder.ComplexType<Details>();
  • 数据标注 [ComplexType]

但考虑使用字符串并相应地管理公共(public) get/set 的替代方案

关于c# - 没有模型属性的 Entity Framework 映射结构/复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21211178/

相关文章:

C# cmd.ExecuteScalar() : "Cannot continue the execution because the session is in the kill state."

c# - RPG 与 C# 和 SQL 的比较

c# - 使用 ADO.NET Entity Framework 时出错

c# - 检查共享网络驱动器上的文件是否可访问

entity-framework - EntityFramework.IBM.DB2 无法连接到 db2

mysql - EF Core MySQL Contains(variable) like operator

c# - 使用 Json.NET 的流利转换器/映射器?

php - 如何在 Laravel 4 中使用参数和 CASE 语句进行更新查询

nhibernate - 流利的nhibernate自动映射版本列

c# - 如何在 asp.net 中使用 System.Web.Caching?