gpt4 book ai didi

asp.net - 找不到适合实体类型 'Uri' 的构造函数。

转载 作者:行者123 更新时间:2023-12-03 21:21:56 27 4
gpt4 key购买 nike

我有这个模型:

public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public Uri Link { get; set; }
}

我把它加到 fluent-api使用以下代码:
 builder.Entity<Book>(entity => {
entity.HasKey(b => b.Id);
});

当我运行这个:
add-migration InitialMigration -context MyAppContext

然后我得到:

No suitable constructor found for entity type 'Uri'. The following parameters could not be bound to properties of the entity: 'uriString', 'uriString', 'dontEscape', 'baseUri', 'relativeUri', 'dontEscape', 'uriString', 'uriKind', 'baseUri', 'relativeUri', 'serializationInfo', 'streamingContext', 'baseUri', 'relativeUri', 'flags', 'uriParser', 'uri'.

最佳答案

你不能持久化 Uri直接到数据库,因为没有关联的 SQL 类型。您需要改用字符串。如果您使用的是 View 模型(应该如此),则您的 View 模型可以具有 Uri ,然后您只需要在映射到实际实体类型期间获取字符串表示。

您还可以简单地利用 EF Core 的值(value)转换(在 2.1+ 中可用)。见 docs了解更多详情。本质上,在您流畅的配置中:

.HasConversion(
v => v.ToString(),
v => new Uri(v));

关于asp.net - 找不到适合实体类型 'Uri' 的构造函数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52964200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com