Skip to main content
edited body
Source Link
haim770
  • 49.2k
  • 7
  • 112
  • 139

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework context doesn't contain a parameter-less constructor, iI assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, iI would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework context doesn't contain a parameter-less constructor, I assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, I would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}
edited body
Source Link
haim770
  • 49.2k
  • 7
  • 112
  • 139

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework Contextcontext doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework Context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}
deleted 4 characters in body
Source Link
haim770
  • 49.2k
  • 7
  • 112
  • 139

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework Context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : ObjectContextDbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework Context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : ObjectContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}

Your question can be rewritten as "How to pass an argument to a generic type constructor with a new() constraint".

From MSDN:

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.

Since the bare Entity Framework Context doesn't contain a parameter-less constructor, i assume your EMDataContext is your custom context that derives from it:

public class EMDataContext : DbContext
{
      // parameterless ctor, since you're using new() in UnitOfWork<TContext>
      public EMDataContext() : base(???)
      {
      }

      public EMDataContext(string connectionString) : base(connectionString)
      {
      }
}

Now, i would argue that your EMDataContext is incapable of having a parameter-less constructor, hence incapable of the new() constraint as well, especially when you're saying that you do want to pass a connection-string parameter.

Try to change your UnitOfWork to accept an already-initialized context in it's constructor (common pattern):

public class UnitOfWork<TContext>
{
    public UnitOfWork(TContext ctx)
    {
        _ctx = ctx;
    }
}

Alternatively (if you still want to "fit the pattern"), try to instantiate the context using Activator:

public class UnitOfWork<TContext>
{
    public UnitOfWork(string connectionString)
    {
        _ctx = (TContext)Activator.CreateInstance(typeof(TContext), new[] { connectionString });
    }
}
Source Link
haim770
  • 49.2k
  • 7
  • 112
  • 139
Loading