Starcounter / Starcounter.Linq

MIT License
1 stars 4 forks source link

Cannot access Simplified.Ring1.Something.Name field #20

Closed un-tone closed 6 years ago

un-tone commented 6 years ago

I got the following error

System.MissingFieldException: Поле не найдено: "Simplified.Ring1.Something.Name".

when call something like the following:

DbLinq.Objects<WebTemplateGroup>().Where(x => x.Name != "")

DbLinq.Objects<WebTemplateGroup>()
                .Where(x => x.Deleted == false)
                .OrderBy(x => x.Name)

At first sight I can note that unlike other class members Name is a field, when others is properties. But using Starcounter.Linq tests I didn't prove that this is the reason.

un-tone commented 6 years ago

It looks like the error happens in some static constructor.

miyconst commented 6 years ago

It looks like the error happens in some static constructor.

What is the stack trace? Using static constructors with the database classes is not a good idea.

miyconst commented 6 years ago

At first sight I can note that unlike other class members Name is a field, when others is properties.

The future versions of Starcounter will not allow fields in the database classes, only properties. So, if it makes life easier, you may limit LINQ to SQL provider to support properties only, and throw exception when a field provided.

un-tone commented 6 years ago

The stack trace is quite helpless.

I put the following code in the beginning of Program.Main:

var data = DbLinq.Objects<WebTemplateGroup>().OrderBy(x => x.Name);

and got the following:

Starcounter.DbException: ScErrFailingEntrypoint (SCERR4259): The user code entrypoint method raised an exception. System.MissingFieldException: Поле не найдено: "Simplified.Ring1.Something.Name".
   в WebsiteEditor.Program.Main()
   в Starcounter.Hosting.Package.ExecuteEntryPoint(Application application, Assembly assembly) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 530
Version: 2.3.1.7934.
Help page: https://docs.starcounter.io/v/2.3.1/?q=SCERR4259.
   в Starcounter.Hosting.Package.ExecuteEntryPoint(Application application, Assembly assembly) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 564
   в Starcounter.Hosting.Package.ProcessWithinCurrentApplication(Application application, ApplicationDirectory applicationDir) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 278
   в Starcounter.Hosting.Package.Process() в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 139
   в Starcounter.Hosting.Package.Process(IntPtr hPackage) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 56
   в StarcounterInternal.Hosting.Processor.RunMessageLoop(Void* hsched) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Processor.cs:строка 89
ErrorCode=4259
HResult=-2146233088
HelpLink=https://docs.starcounter.io/v/2.3.1/?q=SCERR4259
---> System.MissingFieldException: Поле не найдено: "Simplified.Ring1.Something.Name".
   в WebsiteEditor.Program.Main()
   в Starcounter.Hosting.Package.ExecuteEntryPoint(Application application, Assembly assembly) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Hosting\Starcounter.Hosting\Package.cs:строка 530
HResult=-2146233071

I put the same code in a view-model and got the following:

System.MissingFieldException: Поле не найдено: "Simplified.Ring1.Something.Name".
   в WebsiteEditor.ViewModels.SurfaceGroupsPage.Init()
   в Starcounter.Authorization.Routing.Router.DefaultPageCreator(RoutingInfo routingInfo) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 25
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass10_0.<RunResponse>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 107
   в Starcounter.Authorization.Routing.Router.TerminalMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 129
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass11_0.<RunWithMiddleware>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Middleware.ContextMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\joozek\dev\authorization\Authorization\Routing\Middleware\ContextMiddleware.cs:строка 32
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass11_0.<RunWithMiddleware>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Db.<>c__DisplayClass50_0`1.<Scope>b__0() в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter\Db.cs:строка 541
   в Starcounter.Internal.TransactionManager.Scope(TransactionHandle handle, Action action) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter\TransactionManager.cs:строка 551
   в Starcounter.Transaction.Scope(Action action) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter\Transaction.cs:строка 224
   в Starcounter.Db.Scope(Action action, Boolean isReadOnly) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter\Db.cs:строка 518
   в Starcounter.Db.Scope[TResult](Func`1 func, Boolean isReadOnly) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter\Db.cs:строка 542
   в Starcounter.Authorization.Routing.Middleware.DbScopeMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\joozek\dev\authorization\Authorization\Routing\Middleware\DbScopeMiddleware.cs:строка 19
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.RunResponse(Type pageType, Request request, String[] arguments) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 105
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass8_0.<HandleGet>b__0(Request request) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 69
   в Starcounter.Rest.UserHandlerInfo.RunUserDelegate(Request req, IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Rest\UriHandlersManager.cs:строка 201
   в Starcounter.Internal.Web.AppRestServer.RunDelegateAndProcessResponse(IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack, Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Apps.JsonPatch\AppRestServer.cs:строка 96
   в Starcounter.Rest.UriManagedHandlersCodegen.RunUriMatcherAndCallHandler(String methodSpaceUriSpace, String methodSpaceUriSpaceLower, Request req, UInt16 portNumber, Response& resp) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Rest\UriManagedHandlersCodegen.cs:строка 1287
   в Starcounter.Self.DoSelfCall(UInt16 portNumber, String method, String relativeUri, Dictionary`2 headersDictionary, String body, Byte[] bodyBytes, HandlerOptions handlerOptions, Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Internal\Rest\Self.cs:строка 440
   в Starcounter.Self.GET(String uri) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Internal\Rest\Self.cs:строка 82
   в WebsiteEditor.Api.Authorization.AuthorizedHandlers.<CreateApiRouter>b__2_0(RoutingInfo info) в C:\Users\untone\Documents\Projects\Starcounter\Git\Website\src\WebsiteEditor\Api\Authorization\AuthorizedHandlers.cs:строка 85
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass10_0.<RunResponse>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 107
   в Starcounter.Authorization.Routing.Router.TerminalMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 129
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass11_0.<RunWithMiddleware>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в WebsiteEditor.Api.Authorization.MasterPageMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\untone\Documents\Projects\Starcounter\Git\Website\src\WebsiteEditor\Api\Authorization\MasterPageMiddleware.cs:строка 14
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass11_0.<RunWithMiddleware>b__0() в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Middleware.SecurityMiddleware.Run(RoutingInfo routingInfo, Func`1 next) в C:\Users\joozek\dev\authorization\Authorization\Routing\Middleware\SecurityMiddleware.cs:строка 30
   в Starcounter.Authorization.Routing.Router.RunWithMiddleware(RoutingInfo routingInfo, IEnumerable`1 middlewares) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 115
   в Starcounter.Authorization.Routing.Router.RunResponse(Type pageType, Request request, String[] arguments) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 105
   в Starcounter.Authorization.Routing.Router.<>c__DisplayClass8_0.<HandleGet>b__0(Request request) в C:\Users\joozek\dev\authorization\Authorization\Routing\Router.cs:строка 69
   в Starcounter.Rest.UserHandlerInfo.RunUserDelegate(Request req, IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Rest\UriHandlersManager.cs:строка 201
   в Starcounter.Internal.Web.AppRestServer.RunDelegateAndProcessResponse(IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack, Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Apps.JsonPatch\AppRestServer.cs:строка 96
   в Starcounter.Rest.UriManagedHandlersCodegen.RunUriMatcherAndCallHandler(String methodSpaceUriSpace, String methodSpaceUriSpaceLower, Request req, UInt16 portNumber, Response& resp) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Rest\UriManagedHandlersCodegen.cs:строка 1287
   в Starcounter.Self.DoSelfCall(UInt16 portNumber, String method, String relativeUri, Dictionary`2 headersDictionary, String body, Byte[] bodyBytes, HandlerOptions handlerOptions, Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Internal\Rest\Self.cs:строка 440
   в Starcounter.Self.GET(String uri) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Internal\Rest\Self.cs:строка 82
   в WebsiteEditor.Api.MainHandlers.<>c.<Register>b__0_3() в C:\Users\untone\Documents\Projects\Starcounter\Git\Website\src\WebsiteEditor\Api\MainHandlers.cs:строка 59
   в Starcounter.Rest.UserHandlerInfo.RunUserDelegate(Request req, IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Rest\UriHandlersManager.cs:строка 201
   в Starcounter.Internal.Web.AppRestServer.RunDelegateAndProcessResponse(IntPtr methodSpaceUriSpaceOnStack, IntPtr parametersInfoOnStack, Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Apps.JsonPatch\AppRestServer.cs:строка 96
   в Starcounter.Internal.AppsBootstrapper.ProcessExternalRequest(Request req) в C:\TeamCity\BuildAgent\work\sc-11612\Level1\src\Starcounter.Apps.JsonPatch\AppsBootstrapper.cs:строка 348
HResult=-2146233071

Also this code is unattainable for debugger. It throws the exception on calling the method which contains this code.

miyconst commented 6 years ago

@un-tone what happens if you execute the following queries from the administrator panel?

SELECT Name
FROM Simplified.Ring1.Something

SELECT *
FROM Simplified.Ring1.Something
ORDER BY Name
un-tone commented 6 years ago

It obtains the data as expected, without errors.

miyconst commented 6 years ago

Then there is something strange going on. What is the full query generated by LINQ provider? Can you execute the same query from the admin panel?

un-tone commented 6 years ago

And there is a problem with it, LINQ provider is not able to generate the query and I even can't find the code which raises the error. Very strange.

To be clear it is not possible to achieve this breakpoint: image

miyconst commented 6 years ago

It seems like there is something wrong going on inside the weaver. @per-samuelsson anything you can help with?

per-samuelsson commented 6 years ago

What version of Starcounter is this?

un-tone commented 6 years ago

I've created a demo to reproduce this issue in demo-field-access branch. Steps:

  1. Build a Starcounter.Linq solution
  2. Run the Demo app
  3. Go to http://localhost:8080/field-access
un-tone commented 6 years ago

The latest RC: 2.3.1.7934

per-samuelsson commented 6 years ago

Regarding static constructors, weaver use them to support lazy initialization of kernel handles when types are first used. I don't really like them either, but we haven't found any other way to achieve that so far.

per-samuelsson commented 6 years ago

The latest RC: 2.3.1.7934

OK. I don't know exactly how this LINQ provider will work, but trying to support database fields is really, really asking for trouble IMO. That's why I hoped for 2.4, because there, we will not allow fields any more.

per-samuelsson commented 6 years ago

... but trying to support database fields is really, really asking for trouble IMO.

And, if you didn't know, that is because fields are dropped during weaving, replaced with properties. And that changes the surface. Meaning that if you compile against the non-weaved version (which I assume LINQ does), you'll end up with .ldfld instructions, where operand is no longer present after weaving.

un-tone commented 6 years ago

Thanks for the explanation @per-samuelsson.

So @miyconst, currently Starcounter.Linq is not support database fields officially (I will specify it in the README) and I can close the issue, right?

miyconst commented 6 years ago

Yes, you can close the issue after the documentation is updated.

Is it possible to throw a more user friendly exception in this case?

un-tone commented 6 years ago

I don't see the way to manage with it from Starcounter.Linq.

miyconst commented 6 years ago

@per-samuelsson can you suggest something?

per-samuelsson commented 6 years ago

@per-samuelsson can you suggest something?

Well, if we implement this thing in 2.4 only, the error won't persist at all, because weaving will happen before LINQ compile against the domain model. And also: since we would never even touch fields there, it wouldn't be a compile-time problem anyway.

As for 2.3, I'm not sure, it kind of depends how LINQ driver is implemented I guess. If there is some tree with nodes beneath, and fields could be identified as just fields (and I assume they can), maybe we can raise some warning/error if we detect a LINQ query target such? But again, I really don't know how this works, so hard to say for sure.

miyconst commented 6 years ago

@per-samuelsson thank for the answer.

Looks like there is nothing we can do for 2.3 without spending significant amount of time.

@un-tone please close the issue as soon as the documentation is updated.

un-tone commented 6 years ago

Closing. The info added to the README.