19 Kasım 2016 Cumartesi

HttpPostedFileBase mapping error at Entitiy Framework

'HttpPostedFileBases: EntityType: EntitySet 'HttpPostedFileBases' is based on type 'HttpPostedFileBase' that has no keys defined.'

This error comes with object by HttpPostedFileBase class.I am using that type to perform my processes about image files.There is sample an image class below :

  public enum UsageType
    {
        Logo,
        Profil
    }

    public class Image : BaseObject
    {
        public const int MAX_IMG_SIZE = 3072;
        public static readonly string[] ALLOWED_IMG_TYPES = { "image/jpeg", "image/png" };

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
        [ReadOnly(true)]
        [DisplayName("Size")]
        public double Size { get; set; }

        [ReadOnly(true)]
        [DisplayName("Type")]
        public string Type { get; set; }

        [Required]
        [DisplayName("Alt")]
        public string Alt { get; set; }

        [Required]
        [DisplayName("File")]
        [NotMapped]
        public HttpPostedFileBase File { get; set; }

        [DisplayName("Data")]
        public byte[] Data { get; set; }

        [DisplayName("Base64")]
        public string Base64String { get; set; }

        [DisplayName("Src")]
        public string Src { get; set; }

        [DisplayName("Usage Type")]
        public UsageType UsageType { get; set; }
........................

Solution : In Entitiy Framework when you update your schema the error appears on package manager console which is I have mentioned above.You need HttpPostedFileBase object to catch image file.For that reason just type that annotation inside the code below colored blue(Not Mapped).After that migration system wont try to convert this File property to a database column.We use file object to fill other fields of our class such as size,data.etc.

Entitiy Framework 'The Name value should be a comma separated list of foreign key property names' Error

'The ForeignKeyAttribute on property 'Owner' on type 'MyProject.Models.Comment' is not valid. The foreign key name 'OwnerId' was not found on the dependent type 'MyProject.Models.Comment'. The Name value should be a comma separated list of foreign key property names.'

I have encountered that error above while i was uploading my database using package manager console.(update-database process on consele)

My problem is about with two classes named User and Comment.There is one to many relation between them.Each user can have comments more than one and every comment belongs to an owner(user).Below are my classes:

   public class Comment : BaseObject
    {
        [ForeignKey("OwnerId")]
        public virtual User Owner { get; set; }

        public Guid OwnerId;

        public virtual ICollection<Comment> Replies{ get; set; }
    }

  public class User : BaseObject
    {
       public virtual ICollection<Comment> Comments { get; set; }
    }

Solution 1 :

In such cases be sure that your foreign key is available as primitive(int,guid,etc) and sign it using data annotation that is [ForeignKey] on related object property.In this sample you have to write like that :

        [ForeignKey("OwnerId")]
        public virtual User Owner { get; set; }

        public Guid OwnerId;

! Don't forget that which key is going to be used  for Owner object.

Solution 2 :

And also you need to declare your primitive key carrier as property.Not like above.

public Guid OwnerId{get;set;}