3

I have a problem with EF 4.1

Here are my entities and context:

 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; }
    }
}

at mvc controller I have

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

When I run my code, I get exception:

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.

Any ideas on how to get around this?

CAbbott
  • 8,078
  • 4
  • 31
  • 38
user278618
  • 19,306
  • 42
  • 126
  • 196

1 Answers1

3

It sounds like EF doesn't know what to do with Uri type; it has no SQL mapping. Your best bet would be to change the Link property from Uri to string.

Frank Rosario
  • 2,512
  • 5
  • 31
  • 47
  • 4
    You can wrap the Uri property in a [NotMapped] around the underlying db String so that it remains convenient to use as a Uri object in class usage, but still stores fine with EF. – Chris Moschini Sep 08 '12 at 20:01