Entity infrastructure key not defined

I have a class like this

     public class Phone : ItemBase
    {
        public virtual string Model { get; set; }

        public virtual decimal Price { get; set; }

        [ScaffoldColumn(false)]
        public virtual string ImagePath { get; set; }

        public virtual string Network { get; set; }

        [DisplayName("Dimensions")]
        public virtual string BodyDimension { get; set; }
}

      

and a base class like this

 public class ItemBase
{
    [ScaffoldColumn(false)]
    [Key]
    public virtual long ItemID;

    [ScaffoldColumn(false)]
    public virtual DateTime CreatedDate { get; set; }

    [ScaffoldColumn(false)]
    public virtual DateTime ModifiedDate { get; set; }
}

      

the key is defined in the base class, but EF says

Phone :: Object type "Phone" has no key. Define a key for the entity type.

Should I move the key field to the phone class? What's the best solution?

+3


source to share


1 answer


  • You don't need to mark virtual fields ItemId

  • make ItemId

    property instead of fields.

something like that:



    public class ItemBase
    {
        [ScaffoldColumn(false)]
        [Key]
        public long ItemID {get; set;}

        [ScaffoldColumn(false)]
        public virtual DateTime CreatedDate { get; set; }

        [ScaffoldColumn(false)]
        public virtual DateTime ModifiedDate { get; set; }
    }

      

+2


source







All Articles