Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

List.Add() Ambiguous

$
0
0

Hi

I have a base class:

namespace SportsStore.Models.Entities.Base
{
    public abstract class EntityBase
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Timestamp]
        public byte[] TimeStamp { get; set; }
    }
}

and Interface repository:

namespace SportsStore.Models.Repos.Base
{
    public interface IRepo<T> where T : EntityBase
    {
        int Count { get; }
        bool HasChanges { get; }
        T Find(int? id);
        T GetFirst();
        IEnumerable<T> GetAll();
        IEnumerable<T> GetRange(int skip,int take);
        int Add(T entity, bool persist = true);
        int AddRange(IEnumerable<T> entities, bool persist = true);
        int Update(T entity, bool persist = true);
        int UpdateRange(IEnumerable<T> entities, bool persist = true);
        int Delete(T entity, bool persist = true);
        int DeleteRange(IEnumerable<T> entities, bool persist = true);
        int Delete(int id, byte[] timeStamp, bool persist = true);
        int SaveChanges();
    }
}

which says T of  method:

int Add(T entity, bool persist = true);

should be an 'EntityBase' type, I defined my entities as:

[Table("Products", Schema = "store")]
    public class Product: EntityBase {
...
}

They should derive from EntityBase, Finally I have a  namespace dedicated to my MVC View Models that contains  model classes that are not derived from EntityBase:

namespace SportsStore.Models.ViewModels
{

    public class GetUploadedImagesModel
    {
        public List<EditImageModel> ImageMetas { get; set; }
    }

    public class EditImageModel
    {
        [Required]
        public string id { get; set; }
        [Required(ErrorMessage ="title is required")]
        public string title { get; set; }
        public string extension { get; set; }
        public string encodedUrl { get; set; }
    }
}

But when I create an instance of these models and for example do:

List<EditImageModel> model = new List<EditImageModel>();

 model.Add(new EditImageModel()
                {
                    id = id,
                    title = title,
                    extension = file.Extension,
                    encodedUrl = encUrl
                });

The compiler will go to the Repository implementation and tries to use its Add method instead of the List<T> collection  Add method, How do I resolve this ambiguity and why should this happen?


Viewing all articles
Browse latest Browse all 9386

Trending Articles