实体类型"Uri"要求定义主键 [英] The entity type 'Uri' requires a primary key to be defined

查看:133
本文介绍了实体类型"Uri"要求定义主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引用了一个通用模型,因此无法控制其中的属性.假设它看起来像这样:

I reference a common model, so I don't have control over the attributes in it. Assuming it looks like this:

  public class Message
  {
      public Guid Id { get; set; }
      public string Sender { get; set; }
      public Uri Uri { get; set; }
  }

其中 Uri System.Uri .

然后在我的上下文中重写 OnModelCreating 以便设置主键:

In my context I then override OnModelCreating in order to set the primary key:

  public DbSet<Message> Messages { get; set; }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity<Message>()
          .HasKey(i => i.Id);
  }

然后我在数据库初始化程序中运行 Initialize :

I then run Initialize in my database initializer:

  public void Initialize()
  {
      _logger.Information("Ensuring database is created.");

      _messageContext.Database.Migrate();
  }

但是出现以下错误:

实体类型"Uri"要求定义主键.

有什么提示可以解决这个问题吗?

Any hints to how I can get around this?

显然,这是一个已知问题.

推荐答案

实体只能包含两种属性类型(EF可以使用):

Entities can only contain two sorts of property types (that can be used by EF):

  1. 映射到原始SQL类型(字符串,int,bool等)的类型.
  2. 在EF中定义的实体类型.

由于 Uri 不会映射到原始SQL类型,因此EF会尝试像对待实体一样对其进行处理.

Since Uri does not map to a primitive SQL type, EF tries to handle it as if it were an entity.

实体存储在单独的表中.要将实体存储在表中,它需要一个唯一的ID.

Entities are stored in a separate table. To store an entity in a table, it needs a unique id.

有一些解决方法.我建议的方法可以维护 Uri ,但可以阻止EF尝试使用它.

There are ways around this. The method I'm suggesting maintains the Uri but prevents EF from trying to use it.

步骤1-将Uri设置为[未映射]

这使得EF 忽略 Uri属性,因为它无法正确处理它.

This makes it so that EF ignores the Uri property, since it can't properly handle it.

[NotMapped]
public Uri Uri { get; set; }

第2步-创建一个EF 应该处理的字符串属性,而不是Uri.

Step 2 - Make a string property that EF should handle instead of the Uri.

字符串值是从(现在隐藏的)Uri 属性派生的,但是EF却没有看到.它只会看到可用的字符串属性.

The string value is derived from the (now hidden) Uri property, but EF doesn't see that. It only sees a usable string property.

public String URL 
{
    get
    {
        return this.Uri.AbsoluteUri;
    }
    set
    {
        this.Uri = new Uri(value);
    }
 }

这应该可以解决问题.在代码中,您可以直接使用 Uri 属性(就像您已经在做的那样),但是您已经设置了实体,因此EF可以与 URL 字符串属性一起使用.
EF检索实体并设置其属性(包括 URL 字符串属性)后,还将隐式创建 Uri 属性,您可以继续使用它.

And that should do the trick. In code, you work with the Uri property directly (like you were already doing), but you've set up your entity so EF works with the URL string property instead.
After EF retrieves the entity and sets its properties (including the URL string property), the Uri property is implicitly also created and you can keep working with it.

这篇关于实体类型"Uri"要求定义主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆