EntityType'Uri'没有定义键

时间:2011-10-06 17:45:26

标签: c# entity-framework-4.1 ef-code-first

我的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.

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

1 个答案:

答案 0 :(得分:3)

听起来EF不知道如何处理Uri类型;它没有SQL映射。最好的办法是将Link属性从Uri更改为string。