I have a method that iterates over a list of commands and saves them to a database via DbContext. B is a collection of WebObjects DbSet objects (example: DbSet<MlaPerson> MlaPersons )
protected void AddRelatedWebObject<A, B>(A mlaObject, B inputObject, List<Guid> guids) where A : WebObject where B : DbSet<WebObject> { foreach (Guid guid in guids) { mlaObject.RelatedWebObjects.Add(inputObject.Find(guid)); _db.SaveChanges(); } }
Using
foreach (ArticleRelationships item in articleRelationships) { MlaArticle article = new MlaArticle(); article = _db.MlaArticles.Include(m => m.WebSite).Where(m => m.Id == item.ArticleId).First(); AddRelatedWebObject<MlaArticle, DbSet<MlaPerson>>(article, _db.MlaPersons, item.PersonIds); }
_db.MlaPersons are defined as :
public class ECM2Context : DbContext { public DbSet<MlaPerson> MlaPersons { get; set; } }
and MlaPerson is defined as :
public class MlaPerson : WebObject, IValidatableObject { ... }
I thought that calling B was a DbSet<WebObject> would work because the MlaPerson base class is WebObject, but I'm wrong. I get an error message:
The type 'System.Data.Entity.DbSet<ExternalContentManager.Models.MlaPerson>' cannot be used as a type parameter 'B' in the generic type or method 'AddRelatedWebObjects'. There is not implicit reference conversion from 'System.Data.Entity.DbSet<ExternalContentManager.Models.MlaPerson>' to 'System.Data.Entity.DbSet<ExternalContentManager.Models.WebObject>'
I would really appreciate any help offered. Thank you for your help. IN
source share