The type initializer

Posts   
 
    
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 15-Oct-2009 18:27:02   

Greetings,

after execute this query

it throw The type initializer for 'BNIS.AMS.DAL.FactoryClasses.EntityFactoryFactory' threw an exception.

using (var adapter = DataAccess.DataAdapterFactory.Create()) { LinqMetaData metaData = new LinqMetaData(adapter); var q = from c in metaData.SysDBRelation select c;

           var li = q.ToList();
      }

stack trace


   at BNIS.AMS.DAL.FactoryClasses.EntityFactoryFactory.GetFactory(Type typeOfEntity)
   at BNIS.AMS.DAL.FactoryClasses.ElementCreator.GetFactoryImpl(Type typeOfEntity) in C:\Projects\AMS\Solution\BNIS.AMS.DAL\DatabaseGeneric\FactoryClasses\EntityFactories.cs:line 43197
   at BNIS.AMS.DAL.FactoryClasses.ElementCreator.GetFactory(Type typeOfEntity) in C:\Projects\AMS\Solution\BNIS.AMS.DAL\DatabaseGeneric\FactoryClasses\EntityFactories.cs:line 43122
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteEntityProjection(QueryExpression toExecute)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression)
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute()
   at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at BNIS.AMS.BL.EntityDB.GetDBObjectName(String relationName) in C:\Projects\AMS\Solution\BNIS.AMS.BL\Globals\EntityDBMapping.cs:line 46
   at BNIS.AMS.Win.Form1.kryptonButton1_Click(Object sender, EventArgs e) in C:\Projects\AMS\Solution\BNIS.AMS.Win\Form1.cs:line 39
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at ComponentFactory.Krypton.Toolkit.KryptonButton.OnClick(EventArgs e)
   at ComponentFactory.Krypton.Toolkit.KryptonButton.b(Object A_0, MouseEventArgs A_1)
   at ComponentFactory.Krypton.Toolkit.ButtonController.OnClick(MouseEventArgs e)
   at ComponentFactory.Krypton.Toolkit.ButtonController.MouseUp(Control c, Point pt, MouseButtons button)
   at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
   at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
   at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
   at ComponentFactory.Krypton.Toolkit.ViewBase.MouseUp(Point pt, MouseButtons button)
   at ComponentFactory.Krypton.Toolkit.ViewManager.MouseUp(MouseEventArgs e, Point rawPt)
   at ComponentFactory.Krypton.Toolkit.VisualControlBase.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at ComponentFactory.Krypton.Toolkit.VisualControlBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

using VS 2008, SQL 2008, Adapter mode, Shared templates.NET2.0 SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll version 2.6.9.1008, presets Adapter.General2008

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 16-Oct-2009 10:06:12   

I can't find anything in the generated code that can throw such exception. So maybe there were some corrupt compiles IL.

Would you please make sure you rebuild the entire solution and see if the exception goes away.

I als recommend using VS SP1.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 16-Oct-2009 10:21:07   

The static constructor of the entity factory failed it seems. Could you please place a breakpoint in that constructor (in the EntityFactories.cs file) and check why it fails?

Frans Bouma | Lead developer LLBLGen Pro
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 16-Oct-2009 16:49:21   

France.. Yes. I got this exception. it occurs when EntityType is Unknow as my friend Omar posted http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=16697


static EntityFactoryFactory()
        {
            Array entityTypeValues = Enum.GetValues(typeof(BNIS.AMS.DAL.EntityType));
            foreach(int entityTypeValue in entityTypeValues)
            {
                IEntity2 dummy = GeneralEntityFactory.Create((BNIS.AMS.DAL.EntityType)entityTypeValue);
                _factoryPerType.Add(dummy.GetType(), dummy.GetEntityFactory());
            }
        }

Is it right to write logic to bypass Unknow entityType, or you use it in other places?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 19-Oct-2009 11:39:29   

it occurs when EntityType is Unknow as my friend Omar posted http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=16697

AFAIK, EntityType can't be unknown, have you implemented this yourself? Did you or Omar add a Type of Unknown = 0 to the entityType enum?

omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 19-Oct-2009 13:25:44   

Yes Walaa. As Mohammad explained, he traced the exception to the fact that the EntityType enum has the value Unknown=0 which is ofcourse NOT mapped to any actual entity type.

What Mohmmad needs to have is for LLBL Linq provider to handle the case when an EntityType value is NOT mapped to an entity type.

mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 19-Oct-2009 13:38:09   

Or instead of the LLBL Linq provider, do I need to modify the EntityFactory() code to handle the case of the Unknown value ??

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 19-Oct-2009 15:36:06   

will look into it.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 19-Oct-2009 16:47:27   

Could you explain please how you end up with an EntityType enum value which isn't a valid value for an entity? Did you change the templates that valid entities start with 1 ?

(as for every entity in the model, there's a valid EntityType value and a valid factory entry in GeneralEntityFactory. )

Frans Bouma | Lead developer LLBLGen Pro
mohamed avatar
mohamed
User
Posts: 136
Joined: 10-Mar-2008
# Posted on: 19-Oct-2009 17:10:07   

Just add Unknown =0, in SD_ConstantsEnumsTemplate


<[If HasEntity]>
    /// <summary>
    /// Enum definition for all the entity types defined in this namespace. Used by the entityfields factory.
    /// </summary>
    public enum EntityType:int
    {
        Unknown = 0,
<[Foreach Entity CommaCrLf]>        ///<summary><[CurrentEntityName]></summary>
        <[CurrentEntityName]>Entity<[NextForeach]>
    }
<[EndIf]>

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 20-Oct-2009 09:53:43   

ah, in that case: add an if statement (C#, not template) in entityfactoriesadapter.template, line 186, where you check on '0' and continue if the value is 0.

You have to understand that the generated code isn't checking on the validity of the EntityType value as that enum is generated as well, so it always is correct.

Frans Bouma | Lead developer LLBLGen Pro