constructor on type not found

constructor on type not found

Old forum URL: forums.lhotka.net/forums/t/12795.aspx


ashishsinha24 posted on Friday, November 07, 2014

Hi friends,

Can anybody help me with this error.

 

Result Message:

Test method OrderTracker.Test.Library.OrderEditTest.GetOrder threw exception: 

Csla.DataPortalException: DataPortal.Fetch failed (Exception has been thrown by the target of an invocation.) ---> Csla.Reflection.CallMethodException: OrderEdit.DataPortal_Fetch method call failed ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Constructor on type 'Endoscopy.DalEf.EndoscopyEntities' not found.

Result StackTrace:

at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

   at System.Activator.CreateInstance(Type type, Object[] args)

   at Csla.Data.EF6.DbContextManager`1..ctor(String database, String label, DbCompiledModel model, ObjectContext context)

   at Csla.Data.EF6.DbContextManager`1.GetManager(String database, String label, DbCompiledModel model)

   at Csla.Data.EF6.DbContextManager`1.GetManager(String database)

   at Endoscopy.DalEf.DalManager..ctor() in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.DalEf\DalManager.cs:line 30

 --- End of inner exception stack trace ---

    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)

   at Endoscopy.Dal.DalFactory.GetManager() in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.Dal\System\DalFactory.cs:line 22

   at Endoscopy.Lib.Order.OrderEdit.DataPortal_Fetch(Int32 ordermasterid) in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.Lib\Order\Edit\OrderEdit.cs:line 243

   at lambda_method(Closure , Object , Object[] )

   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Boolean hasParameters, Object[] parameters)

 --- End of inner exception stack trace ---

    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)

   at Endoscopy.Dal.DalFactory.GetManager() in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.Dal\System\DalFactory.cs:line 22

   at Endoscopy.Lib.Order.OrderEdit.DataPortal_Fetch(Int32 ordermasterid) in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.Lib\Order\Edit\OrderEdit.cs:line 243

   at lambda_method(Closure , Object , Object[] )

   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Boolean hasParameters, Object[] parameters)

   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Boolean hasParameters, Object[] parameters)

   at Csla.Reflection.MethodCaller.<CallMethodTryAsync>d__b.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Csla.Reflection.MethodCaller.<CallMethodTryAsync>d__5.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Csla.Reflection.LateBoundObject.<CallMethodTryAsync>d__3.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Csla.Server.SimpleDataPortal.<Fetch>d__7.MoveNext()

 --- End of inner exception stack trace ---

    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

   at System.Activator.CreateInstance(Type type)

   at Endoscopy.Dal.DalFactory.GetManager() in d:\InProcess\Endoscopy.Net\Endoscopy.Net\Endoscopy.Dal\System\DalFactory.cs:line 22

 

 

 

JonnyBee replied on Friday, November 07, 2014

Hi,

Start by verifying that the assembly Endoscopy.DalEf is present in the folder that the UnitTest runner i loading assemblies from.

You proable have no direct references to this assembly so it is likely that this assembly (and it's types) are unavailable.

You can f.ex. use ProcMon from Sysinternals to trace this.

ashishsinha24 replied on Friday, November 07, 2014

Hi JonnyBee ,

 

The DalEf is referenced by my test project. I am attaching my project . See if u can point me in the right direction

 

regards

 

ashish

JonnyBee replied on Saturday, November 08, 2014

Some parameters such as "ConnectionName" and "CompiledModel" must be supplied as parameters to the constructor of your EF context.

And in your case you are supplying the "connectionName" and hence the DbContextManager is looking for that constructor. 

The entire issue is caused by YOU explicitly having a specified default constructor in your context and in your call to DbContextManager you specify a connectionName that requires another constructor to be found.

public EndoscopyEntities()
    : base("name=EndoscopyEntities")
{
}

So how do you exepect this line to be able to call the contructor on your context that allows code to specify connectionName?

ConnectionManager = DbContextManager<EndoscopyEntities>.GetManager("Endoscopy"); 

My recommended solution is to add a new constructor to your context class:

public EndoscopyEntities(string connectionName) : base(connectionName)
{
}

Antoher possible solution is to NOT specify a connectionName to DbContextManager.GetManager as this will use the default contructor on the DbContext.

So this will also work:

public DalManager()
{
    ConnectionManager = DbContextManager<EndoscopyEntities>.GetManager();
}

 

JonnyBee replied on Saturday, November 08, 2014

And just for reference, this is the actual code and the possible contructor being called:

private DbContextManager(string database, string label, DbCompiledModel model, ObjectContext context)
{
  _label = label;
 
  if (model != null)
  {
    if (!string.IsNullOrEmpty(database))
      _context = (C)(Activator.CreateInstance(typeof(C), database, model));
    else
      _context = (C) (Activator.CreateInstance(typeof (C), model));
  }
  else if (context != null)
  {
    _context = (C) (Activator.CreateInstance(typeof (C), context, true));
  }
  else if (string.IsNullOrEmpty(database))
  {
    _context = (C) (Activator.CreateInstance(typeof (C)));
  }
  else
  {
    _context = (C)(Activator.CreateInstance(typeof(C), database));
  }
}

ashishsinha24 replied on Saturday, November 08, 2014

Thanks for the reply. But I still don't get it. I am new to CSLA 4.5

What I did was :

1) Made  a copy of  the sample ProjectTracker application which runs successfully.

2) Update the EF with my database and changed the DalManager.cs respectively with my database name. 

3) I didn't write a line of code elsewhere.

So, it should work without a glitch. Now, It has been a week since I have been trying this. 

Also, I tried your suggestion but the Test Failed with the following error.

 

Test Name: GetDoctors

Test FullName: Endoscopy.Test.Library.DoctorListTest.GetDoctors

Test Source: d:\InProcess\Endoscopy.Net\ProjectTracker.Test.Library\DoctorListTest.cs : line 49

Test Outcome: Failed

Test Duration: 0:00:00.8892848

 

Result Message:

Test method Endoscopy.Test.Library.DoctorListTest.GetDoctors threw exception: 

Csla.DataPortalException: DataPortal.Fetch failed (The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715) ---> Csla.Reflection.CallMethodException: DoctorList.DataPortal_Fetch method call failed ---> System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

 

 

regards and thanks for your time,

 

Ashish

 

 

 

 

 

 

 

 

 

JonnyBee replied on Saturday, November 08, 2014

EntityFramework has 2 ways of defining the model:

 

 

(albeit bad names - see http://blogs.msdn.com/b/adonet/archive/2014/10/21/ef7-what-does-code-first-only-really-mean.aspx)

And there is NO DbContext in ProjectTracker. 
ProjectTracker uses the EDMX (and DatabaseFirst) where the context inherits from ObjectContext (and NOT DbContext) and use the Csla.Data.ObjectContextManager.

The Exception you get is caused by the model being generated from and EDMX and you try to use these classes in a DbContext (Code-First).

So you must make sure that to use the correct variant. EDMX will be retired from EF7 so you should probably look into using Code-First.  

This is the actual line of code that causes the exception:

public partial class EndoscopyEntities : DbContext
{
    public EndoscopyEntities()
        : base("name=EndoscopyEntities")
    {
    }
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
===>        throw new UnintentionalCodeFirstException();
    }

ashishsinha24 replied on Saturday, November 08, 2014

Cant we have a more simpler DalEf ? I am going nuts

ajj3085 replied on Saturday, November 08, 2014

It seems like you're trying to mix a few ways of doing EF together; EF supports Code First and Database First (and I think Model First).  You need to pick one way as you can't use a combination.  Your original problem though is simply a missing ctor that had the expected parameters.  The complexity here though is mostly EF; Csla just provides a way to share an existing EF context if there is one.

Copyright (c) Marimer LLC