Skip to content

Custom Value Generator Which is trying to acces DI Service failed #10792

Open
@SPEMoorthy

Description

@SPEMoorthy

Hi,
I am trying to implement a Custom Value generator. Whchi is getting number from other DB.

My code is as follows.

public class MyValueGenerator: ValueGenerator<int>
    {
        private ISeqDefinitionRepository _repository;

        public MyValueGenerator(ISeqDefinitionRepository repository)
        {
            _repository = repository;
        }

        public override bool GeneratesTemporaryValues => false;

        public override int Next(EntityEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            int res = _repository.NextValue(1);
            return res; 
        }
    }


On Model Creating

 private void ConfigureMfr(EntityTypeBuilder<Mfr> builder)
        {
            builder.Property(b => b.MfrId).HasValueGenerator<MyValueGenerator>();
        }

I am getting following Error

Cannot create instance of value generator type 'MyValueGenerator'. Ensure that the type is instantiable and has a parameterless constructor, or use the overload of HasValueGenerator that accepts a delegate.
No parameterless constructor defined for this object.

Requesting to guide met to get the instance of the service in custom ValueGenerator Class.

Further technical details

EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Visual Studio 2017

Thanks & Regards,
Easwar

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions