I am trying to setup a test project using EF and I am getting the following error message:
The entity type Animal is not part of the model for the current context
I've done some Google searching but not much has helped so far.
I have a Zoo database setup which contains 3 tables - Zone, Animal and Keeper. Each Animal has a foreign key to Zone and Keeper and Keeper has a foreign key to Zone. The method that I have used for this project is database first. The models for the database generated successfully and I can access those classes. But I'm not sure how to fix this error. I'll print my classes below. Any help would be much appreciated
Zoo.Context.cs
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<Animal> Animals { get; set; }
public DbSet<Keeper> Keepers { get; set; }
public DbSet<Zone> Zones { get; set; }
}
Cerberus2589
Member
259 Points
144 Posts
The entity type is not part of the model for the current context
Oct 11, 2012 09:49 AM|LINK
I am trying to setup a test project using EF and I am getting the following error message:
I've done some Google searching but not much has helped so far.
I have a Zoo database setup which contains 3 tables - Zone, Animal and Keeper. Each Animal has a foreign key to Zone and Keeper and Keeper has a foreign key to Zone. The method that I have used for this project is database first. The models for the database generated successfully and I can access those classes. But I'm not sure how to fix this error. I'll print my classes below. Any help would be much appreciated
Zoo.Context.cs
using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class Entities : DbContext { public Entities() : base("name=Entities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public DbSet<Animal> Animals { get; set; } public DbSet<Keeper> Keepers { get; set; } public DbSet<Zone> Zones { get; set; } }IRepository.cs
public interface IRepository<TEntity> : IDisposable { void Add(TEntity entity); void Delete(TEntity entity); }IAnimalRepository.cs
public interface IAnimalRepository : IRepository<Animal> { IQueryable<Animal> GetAllByKeeper(Keeper keeper); IQueryable<Animal> GetAllByZone(Zone zone); }AnimalRepository.cs
public class AnimalRepository : IAnimalRepository { private IDbContext _context; public AnimalRepository(IDbContext context) { this._context = context; } public IQueryable<Animal> GetAllByKeeper(Keeper keeper) { throw new System.NotImplementedException(); } public IQueryable<Animal> GetAllByZone(Zone zone) { throw new System.NotImplementedException(); } public void Add(Animal entity) { _context.Set<Animal>().Add(entity); } public void Delete(Animal entity) { _context.Set<Animal>().Remove(entity); } public void Dispose() { if (_context != null) _context.Dispose(); } }IDbContext.cs
public interface IDbContext { IDbSet<TEntity> Set<TEntity>() where TEntity : class; int SaveChanges(); void Dispose(); }ZooContext.cs
public class ZooContext : Entities, IDbContext { public new IDbSet<TEntity> Set<TEntity>() where TEntity : class { return base.Set<TEntity>(); } }Program.cs
public class Program { public static void Main(string[] args) { try { IDbContext context = new ZooContext(); IRepository<Animal> repository = new AnimalRepository(context); Animal animal = new Animal() { Id = 4, Name = "Giraffe", Age = 7, KeeperId = 2, ZoneId = 2 }; repository.Add(animal); context.SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } }thaicarrot
Contributor
5132 Points
1465 Posts
Re: The entity type is not part of the model for the current context
Oct 11, 2012 01:11 PM|LINK
This is because the "Animal" doesn't belong to current context.
Right click at the "Animal" then choose Go to Definition. Make sure that you're not generated other class.
Are you using code first map to existing database?
Weera
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: The entity type is not part of the model for the current context
Oct 13, 2012 01:46 AM|LINK
Hi,
Try to use this below:
_context.Animals.Add(entity);
I'm suspecting it that because the EntityState isn't right. (It has been in Detached, which has caused the problem).
Cerberus2589
Member
259 Points
144 Posts
Re: The entity type is not part of the model for the current context
Oct 25, 2012 08:24 AM|LINK
Thanks for your answers I got this resolved in the end.
nahidf
Member
2 Points
1 Post
Re: The entity type is not part of the model for the current context
Feb 14, 2013 03:32 PM|LINK
please explaine how?
i have the same problem
DukeAmes
Member
66 Points
15 Posts
Re: The entity type is not part of the model for the current context
Feb 26, 2013 01:41 PM|LINK
Please support the community and post your solution. Thank you.
saltug
Member
2 Points
6 Posts
Re: The entity type is not part of the model for the current context
Mar 04, 2013 10:16 AM|LINK
Where did you get this tutorial? I need something similar.