其他分享
首页 > 其他分享> > 实体类型“ Uri”没有定义键

实体类型“ Uri”没有定义键

作者:互联网

我对EF 4.1有问题

这是我的实体和上下文:

 public class PasmISOContext : DbContext
 {
     public DbSet<Avatar> Avatars { get; set; }
     public DbSet<User> Users { get; set; }
 }

namespace PasmISO.Domain
{
    public class User
    {
        [Key]
        public string Login { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public Avatar Avatar { get; set; }
    }
}

namespace PasmISO.Domain
{
    public class Avatar
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public Uri Link { get; set; }
    }
}

在MVC控制器我有

 PasmISOContext db=new PasmISOContext();
 if (db.Avatars.Count()==0)
 {
     db.Avatars.Add(new Avatar() { Link = new Uri("http://www.google.com/somelinktojpg") });
 }

当我运行代码时,出现异常:

One or more validation errors were detected during model generation:

    System.Data.Edm.EdmEntityType: : EntityType 'Uri' has no key defined. Define the key for this EntityType.
    System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Uris� is based on type �Uri� that has no keys defined.

关于如何解决这个问题的任何想法?

解决方法:

听起来像EF不知道该如何处理Uri类型.它没有SQL映射.最好的选择是将Link属性从Uri更改为string.

标签:ef-code-first,entity-framework-4-1,c
来源: https://codeday.me/bug/20191202/2086998.html