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;
            }
        }
 
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.

Hi @jmcilhinney I have updated the code as you suggested, getting this below error when I call this controller like this.

protected BaseLinkedEntityEditorController<TEntity> _baseController;

Error :
TEntity' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TEntity' in the generic type or method 'BaseLinkedEntityEditorController<TEntity>


updated method.
C#:
 private TEntity GetNewTrackedEntity()
        {
            TEntity _model;

            try
            {
                _model = new TEntity(); //(TEntity)EntitySet;
                //_model = EntitySet;
                return _model;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                throw;
            }
        }
 
I have updated the code as you suggested

Have you really? Did you add the constraint that I demonstrated in the first code snippet of post #8?
 
Have you really? Did you add the constraint that I demonstrated in the first code snippet of post #8?

Yes @jmcilhinney, I didn't add below line in previous comment.

C#:
public abstract class BaseLinkedEntityEditorController<TEntity> : BaseEditorController where TEntity : BaseEntity,new()

    {

        #region MVC Interface



        // GET: /<Model>Editor/New
 
Does your entity have a parameterless constructor, either implicit or explicit?

This is parameterless constructor. Actually this BaseAggregatedEditorController class is partial class. I added new() in both the files. now error gone.

Build was success, But when I call below code record not saving in database.

C#:
  _ORM.Add(model);
                      var i = await _ORM.SaveChangesAsync();
 
That doesn't seem to make sense. The code you showed us doesn't include the partial keyword, so I don't see how the class could be partial unless you're not actually showing us the code you really have. If they really are partial classes then there should be no need to specify the generic constraints on both parts.
 
That doesn't seem to make sense. The code you showed us doesn't include the partial keyword, so I don't see how the class could be partial unless you're not actually showing us the code you really have. If they really are partial classes then there should be no need to specify the generic constraints on both parts.

Hi @jmcilhinney I think you have the source code that I have shared with you. In that Framework folder we have BaseAggregatedEditorController.cs file this is partial class.
 
I'm not going to download source files or any files unless I absolutely have to. You should be posting relevant code directly and what you post should reflect the actual code you're using. Downloads should only be necessary where large amounts of code are required, which they're not here.
 
I'm not going to download source files or any files unless I absolutely have to. You should be posting relevant code directly and what you post should reflect the actual code you're using. Downloads should only be necessary where large amounts of code are required, which they're not here.

Hi @jmcilhinney thanks for your support. All the problem with my DBContext class object. I could able to resolve this issue. Now I am able to save records database.

Thank you very much @Skydiver and @jmcilhinney for your grate support.
 
Back
Top Bottom