Resolved Cannot convert type Microsoft.EntityFrameworkCore.DbSet<TEntity> to TEntity

Himasankar

Member
Joined
Jun 9, 2023
Messages
20
Programming Experience
5-10
Hi, I am getting following issue in GetNewTrackedEntity() in the given code.
in this line _model = (TEntity)EntitySet;

Issue :
Cannot convert type Microsoft.EntityFrameworkCore.DbSet<TEntity> to TEntity

C#:
private TEntity GetNewTrackedEntity()
        {
            TEntity _model;

            try
            {
                _model = (TEntity)EntitySet;
                return _model;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                throw;
            }
        }


  protected Microsoft.EntityFrameworkCore.DbSet<TEntity> EntitySet
        {
            get
            {
                Type _type;
                Microsoft.EntityFrameworkCore.DbSet<TEntity> _set;
               
               _type = this.GetEntityType();
                _set = _ORM.Set<TEntity>();
                return _set;
            }
        }
 
Why would you expect it to work?

It's like having a carton of eggs and trying to treat it like a single egg.
 
Why would you expect it to work?

It's like having a carton of eggs and trying to treat it like a single egg.

Hi,

Actually I would like to get new Tracked entity and fill the data in to that and save it to the database. Existing framework working like that. Now I am migrating existing Asp.net MVC 5 project to Asp.net MVC Core. So, in that process I am getting these issues.
 
You removed your shared file (from your other thread) with the old version of your code so that I can see what the old code used to do. I have serious doubts that even the old Entity Framework would let you cast a DbSet to a single instance of an object. I suspect that you made an error in your porting to EF Core.
 
You removed your shared file (from your other thread) with the old version of your code so that I can see what the old code used to do. I have serious doubts that even the old Entity Framework would let you cast a DbSet to a single instance of an object. I suspect that you made an error in your porting to EF Core.

Hi @Skydiver thank you for quick reply. And sorry for the inconvenience, actually I have removed that file from google drive, because I marked that thread as Resolved. I uploaded again updated my source code here. Please try now.

you can find below code in this file.
Areas->Framework->Controllers-> BaseLinkedEntityEditorController.cs

C#:
   private TEntity GetNewTrackedEntity()
        {
            TEntity _model;

            try
            {
                _model = (TEntity)EntitySet.EntityType;

                return _model;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                throw;
            }
        }
        }


 
Last edited:
A DbSet is like a collection. You wouldn't expect to be able to use a List<string> where a string was expected, would you? I would hope not. In that case, why would you expect to be able to use a DbSet<TEntity> where a TEntity was expected? If you want to get a single entity out of a DbSet then you need to query it. That's the whole point of EF: the DbContext represents the database and each DbSet represents a table and you then query those tables to get individual records, represented by entity instances.
 
A DbSet is like a collection. You wouldn't expect to be able to use a List<string> where a string was expected, would you? I would hope not. In that case, why would you expect to be able to use a DbSet<TEntity> where a TEntity was expected? If you want to get a single entity out of a DbSet then you need to query it. That's the whole point of EF: the DbContext represents the database and each DbSet represents a table and you then query those tables to get individual records, represented by entity instances.

Hi @jmcilhinney

I am using below code. I would like to get new mode from this method.

_model = this.GetNewTrackedEntity();


C#:
  TEntity _model = null;
            EntityEntry _entry = null;

            
                //Adding the new model as shown below invokes EF's add management code.
                //That is a good thing.
                ////Create a new model. 

                _model = this.GetNewTrackedEntity();

                 ////Get the EF entry manager for the new model.
                 _entry = _ORM.Entry(_model);
  model.ChangedBy = userAccount;
 _entry.CurrentValues.SetValues(model);
                _entry.State = Microsoft.EntityFrameworkCore.EntityState.Added;
 
If the point here is to create a new instance of the entity type for the controller then I would think the thing to do would be add the new() constraint to your generic type parameter and then use that constructor:
C#:
public abstract class BaseLinkedEntityEditorController<TEntity> : BaseEditorController where TEntity : BaseEntity, new()
and
C#:
public TEntity GetNewUntrackedEntity()
{
    return new TEntity();
}
I'm assuming that there's no reason that any entity type would ever not have a parameterless constructor. I'm not sure that EF would even allow that anyway.
 
And sorry for the inconvenience, actually I have removed that file from google drive, because I marked that thread as Resolved. I uploaded again updated my source code here.

Oh! I thought that the code you had previously attached was the old version of that code before you started trying to port to EF Core. I was hoping to see how the old code magically got a single entity from a DbSet.
 
Oh! I thought that the code you had previously attached was the old version of that code before you started trying to port to EF Core. I was hoping to see how the old code magically got a single entity from a DbSet.

Hi @Skydiver Before porting to EF Core, we have the code like below.

C#:
   private BaseEntity GetNewTrackedEntity()
        {
            BaseEntity _model;

            try
            {
                _model = (BaseEntity)EntitySet.Create();

                return _model;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                throw;
            }
        }

    protected DbSet EntitySet
        {
            get
            {
                Type _type;
                DbSet _set;

                _type = this.GetEntityType();

                _set = _ORM.Set(_type);

                return _set;
            }
        }
 
What was the type of _ORM in the old code?
 
I think I may owe you an apology as I think I misinterpreted your original question. I thought you were trying to create a DbSet, rather than have a DbSet create an entity. I've never actually used that Create method in old EF, so I blame my lack of familiarity with it.

Looking at the new EF Core DbSet, I don't see any direct equivalent so I'm guessing that you're intended to create a new entity using the type constructor, as I've shown above. I think I'll take a look at the source for that old Create method and see if that gives any clue to some better alternative.
 
I think I may owe you an apology as I think I misinterpreted your original question. I thought you were trying to create a DbSet, rather than have a DbSet create an entity. I've never actually used that Create method in old EF, so I blame my lack of familiarity with it.

Looking at the new EF Core DbSet, I don't see any direct equivalent so I'm guessing that you're intended to create a new entity using the type constructor, as I've shown above. I think I'll take a look at the source for that old Create method and see if that gives any clue to some better alternative.

Hi @jmcilhinney creating new entity using entity type not supporting in non generic controllers, So, I have updated Base controller as generic as you suggested. Then I am getting DBSet<TEntity>. Now I would like to get TEntity from DBSet<TEntity>.
 
Now I would like to get TEntity from DBSet<TEntity>.

As far as I can tell, you can't. Your original question was how to do that using an alternative to DbSet.Create from EF6 and earlier, which I didn't understand at the time but I now do. The reason you can't find an alternative is because, as far as I can tell, there isn't one. I looked at the source code and that Create method makes a call to the DbContext to create the object but that method doesn't exist either. I've always just created an entity directly and you can do so too. I've shown you how to do that using generics, so you only have to do it in one place. As far as I can tell, that's how you're intended to do it.
 
Back
Top Bottom