Db.LoadSelect throws NullReferenceException - servicestack

Starting to pull my hair out over this, so thought I might try asking here.
I have this simple service:
public class EventService : Service
{
public object Get(GetEvents request)
{
return Db.LoadSelect<Event>();
}
}
The LoadSelect() throws a NullReferenceException.
I actually had it working perfectly earlier, but have no idea what is now causing it to throw the exception.
Have tracked it down to this line that is actually throwing the exception within ServiceStack (ServiceStack.OrmLite.Support.LoadList):
LoadList-NullReferenceException
But looking at the locals, everything seems fine to me. I mean, it can obviously get the data from the database, so that part is fine, the data is correct, and it even seems to resolve the ForeignKeys, and everything.
So what value it can't get, or isn't set, I have no idea :/
Any tips on how to troubleshoot this further would be great! =)
[EDIT1 - Here are the POCOs]
public class Event
{
[PrimaryKey]
public int Id { get; set; }
public int Timestamp { get; set; }
public int MessageId { get; set; }
[Reference]
public Message Message { get; }
}
public class Message
{
[PrimaryKey]
public int Id { get; set; }
public string Text { get; set; }
}
[EDIT2 - Findings after reading through the dup answer]
As mentioned in the initial question, the basic NullReferenceException troubleshooting has been performed, and I have dug as deep as I can into the ServiceStack framework to try and identify the culprit.
As far as I can see, all variables that are consumed by the call throwing the exception are okey (see screenshot below).
The only one (highlighted in the image below), should be handled by the ServiceStack OrmLite References API, but maybe someone more familier with the framework can comment, but it all looks good to me atleast...
LoadList-NullReferenceException-002
Perhaps I'm using the References API wrong?
The database structure is quite simple:
Event Table
| Event | CREATE TABLE `Event` (
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Timestamp` int(11) NOT NULL,
`MessageId` int(4) unsigned NOT NULL,
PRIMARY KEY (`Id`),
KEY `MessageId_FK` (`MessageId`),
CONSTRAINT `MessageId_FK` FOREIGN KEY (`MessageId`) REFERENCES `Message` (`Id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
mysql> SELECT * FROM Event;
+----+------------+-----------+
| Id | Timestamp | MessageId |
+----+------------+-----------+
| 1 | 1501026747 | 1 |
| 2 | 1501027047 | 1 |
+----+------------+-----------+
Message Table
| Message | CREATE TABLE `Message` (
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Text` varchar(255) NOT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `Id_UNIQUE` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 |
mysql> SELECT * FROM Message;
+----+---------------------------------------------+
| Id | Text |
+----+---------------------------------------------+
| 1 | Someone is at the door! |
| 2 | 1 has acknowledged the notification. |
| 3 | 2 has acknowledged the notification. |
| 4 | 3 has acknowledged the notification. |
+----+---------------------------------------------+
Exception details
System.NullReferenceException occurred
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=ServiceStack.OrmLite
StackTrace:
at ServiceStack.OrmLite.Support.LoadList`2.SetRefSelfChildResults(FieldDefinition fieldDef, ModelDefinition refModelDef, FieldDefinition refSelf, IList childResults) in C:\TeamCity\buildAgent\work\12884bd5feef0ce7\src\ServiceStack.OrmLite\Support\LoadList.cs:line 154

To answer my own question, for some reason, I decided to remove the setter on the reference property of one of the POCOs (Event.Message).
[Reference]
public Message Message { get; }
Should of course have been (and was at some point, which is why it worked for a short time):
[Reference]
public Message Message { get; set; }
No wonder ServiceStack couldn't set the property, and then use it for the References API.

Related

ServiceStack OrmLite wrong SQL?

I have the following class:
public class ProcessInstance
{
[AutoIncrement]
public int Id { get; set; }
[Reference]
public ProcessDefinition ProcessDefinition { get; set; }
public int ProcessDefinitionId { get; set; }
// and more...
}
Then running the following, which looks fine to me:
var q = db.From<ProcessInstance>().Where(inst => inst.ProcessDefinition.Id == id
&& Sql.In(inst.Status, enProcessStatus.READY, enProcessStatus.ACTIVE));
return db.Exists(q);
When I inspect the last command text SQL from the "db" object, it's wrong:
SELECT 'exists'
FROM "ProcessInstance"
WHERE (("Id" = #0) AND "Status" IN (#1,#2))
LIMIT 1
Note that it's filtering on Id instead of ProcessDefinition.Id, which of course is wrong. Don't know why it's doing that -- at least I'd appreciate getting an error instead of just a wrong result.
However, I've found how to fix it: Use ProcessDefinitionId: Where(inst => inst.ProcessDefinitionId == id gives the correct SLQ:
SELECT 'exists'
FROM "ProcessInstance"
WHERE (("ProcessDefinitionId" = #0) AND "Status" IN (#1,#2))
LIMIT 1
Why didn't the first one work? Why is there no error?
OrmLite is designed for providing a typed api around an SQL Expression so that it should be intuitive to determine the SQL generated from a typed Expression. It doesn’t support magic behavior such as querying any nested objects as attempted with the reference complex type property, I.e. you can only query direct column properties as done in your 2nd query.

ORMLite SQL Server Update

I have a table called PODetail with a primary Key of POno and ItemCode and I have the following:
[Route("/podetail/{POno}/{ItemCode}")]
public class UpdatePODetail : IReturn<PODetail> {
public string POno { get; set; }
public string ItemCode { get; set; }
public int ? QtyPend { get; set; }
public decimal ? NewPrice { get; set; }
public bool ? BackOrder { get; set; }
public string ActionCode { get; set; }
public bool ? OpenOrder { get; set; }
}
public class PODetailService : Service {
public object Any(UpdatePODetail request) {
var podetail = Db.SingleFmt<PODetail>("ItemCode = {0} AND POno = {1}", request.ItemCode, request.POno);
// var cap = new CaptureSqlFilter();
try {
Db.Update(podetail);
} catch {
// var sql = string.Join(";\n\n", cap.SqlStatements.ToArray());
}
:
:
try {
Db.Update(podetail);
} catch (Exception ex) {
string error = ex.Message;
}
return podetail;
}
}
I added the Db.Update call at the top just to check to see if there was some issue changing a column, but I get
Violation of PRIMARY KEY constraint 'aaaaaPoDetail_PK'. Cannot insert
duplicate key in object 'dbo.PODetail'.
So then I added the cap = line to see the SQL code which returns
UPDATE "PODetail" SET "NewItemCode"=#NewItemCode, "POno"=#POno, "Vendor"=#Vendor, "ActionCode"=#ActionCode, "Price"=#Price, "NewPrice"=#NewPrice, "CostPrice"=#CostPrice, "QtyOrd"=#QtyOrd, "QtyRcv"=#QtyRcv, "QtySPO"=#QtySPO, "QtyPend"=#QtyPend, "BackOrder"=#BackOrder, "OpenOrder"=#OpenOrder, "OrderDate"=#OrderDate, "InvoiceNo"=#InvoiceNo, "InvoiceVendor"=#InvoiceVendor, "InvoiceDate"=#InvoiceDate, "InvoiceDiscount"=#InvoiceDiscount, "QtyCancel"=#QtyCancel, "Qtylabels"=#Qtylabels, "REOVendor"=#REOVendor, "CurrentRcvQty"=#CurrentRcvQty, "SOPickQty"=#SOPickQty, "SOItem"=#SOItem, "QtyOther"=#QtyOther, "BackOrderCode"=#BackOrderCode WHERE "ItemCode"=#ItemCode
And then it runs fine uncommented -- no exceptions .. if I remove it it gets the Primary Key error
What is the deal -- why do I need that CaptureSqlFilter call -- or what I do I need to change so that it knows both PoNo and ItemCode are primary Keys or the update needs to say WHERE "ItemCode"=#ItemCode AND "POno"=#PONo? It almost seems as if it is trying to do an INSERT vs an UPDATE without the CaptureSqlFilter
Update 1
The documentation said :
Limitations For simplicity, and to be able to have the same POCO class
persisted in db4o, memcached, redis or on the filesystem (i.e.
providers included in ServiceStack), each model must have a single
primary key, by convention OrmLite expects it to be Id although you
use [Alias("DbFieldName")] attribute it map it to a column with a
different name or use the [PrimaryKey] attribute to tell OrmLite to
use a different property for the primary key.
You can still SELECT from these tables, you will just be unable to
make use of APIs that rely on it, e.g. Update or Delete where the
filter is implied (i.e. not specified), all the APIs that end with
ById, etc.
Workaround single Primary Key limitation
A potential workaround to support tables with multiple primary keys is
to create an auto generated Id property that returns a unique value
based on all the primary key fields,
So I tried to add this
public class PODetail {
public string Id { get { return this.ItemCode + "/" + this.POno; } }
public string ItemCode { get; set; }
public string NewItemCode { get; set; }
public string POno { get; set; }
:
}
But when it went to execute :
Db.SingleFmt<PODetail>
It error out with ID not a valid column or column not found or something like that
So I then tried
public class PODetail {
//public string Id { get { return this.ItemCode + "/" + this.POno; } }
[PrimaryKey]
public string ItemCode { get; set; }
public string NewItemCode { get; set; }
[PrimaryKey]
public string POno { get; set; }
:
}
and it worked on the Db.SingleFmt ... and the Db.Update
So then I added back in the CaptureSqlFilter to see what the query looked like and I got
UPDATE "PODetail" SET "NewItemCode"=#NewItemCode, "Vendor"=#Vendor, "ActionCode"=#ActionCode, "Price"=#Price, "NewPrice"=#NewPrice, "CostPrice"=#CostPrice, "QtyOrd"=#QtyOrd, "QtyRcv"=#QtyRcv, "QtySPO"=#QtySPO, "QtyPend"=#QtyPend, "BackOrder"=#BackOrder, "OpenOrder"=#OpenOrder, "OrderDate"=#OrderDate, "InvoiceNo"=#InvoiceNo, "InvoiceVendor"=#InvoiceVendor, "InvoiceDate"=#InvoiceDate, "InvoiceDiscount"=#InvoiceDiscount, "QtyCancel"=#QtyCancel, "Qtylabels"=#Qtylabels, "REOVendor"=#REOVendor, "CurrentRcvQty"=#CurrentRcvQty, "SOPickQty"=#SOPickQty, "SOItem"=#SOItem, "QtyOther"=#QtyOther, "BackOrderCode"=#BackOrderCode WHERE "ItemCode"=#ItemCode AND "POno"=#POno
Which is what I wanted in the first place.
It works but what is the deal can you have the [PrimaryKey] attribute multiple times (it appears so) and also then why didn't the autogenerated Id work? Just wondering if I am missing something or not understanding the documentation correctly.
Oh and sorry for posting in the comments!
what I do I need to change so that it knows both PoNo and ItemCode are
primary Keys
OrmLite's primary limitation is that each Table has a single primary Key.
Also you can use the built-in Profiling or debug logging to view the generated SQL without needing to change code to use CaptureSqlFilter.
I'd also recommend that you don't use the Request DTO for anything other than defining your Service with. You can use the built-in AutoMapping to easily use it to populate your data model.

Error using Merge in Servicestack.OrmLite Sql Server

Using the latest version of https://github.com/ServiceStack/ServiceStack.OrmLite
[Schema("dbo")]
[Alias("ShelvingCount")]
public class ShelvingCount: IHasId<int>
{
[Alias("ShelvingCountId")]
[Index(Unique = true)]
[AutoIncrement]
public int Id { get; set;}
[Required]
[References(typeof(Account))]
public int AccountId { get; set; }
[Reference]
public Account Account { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public int? Status { get; set; }
}
I removed the property EmployeeId which was a foreigh key to the Employees table. And I forgot to remove the "Merge command" in the code below:
var result = await dbCon.SqlListAsync<ShelvingCount>("EXEC getAllShelvingCounts #accountId, #status, #fromDate, #toDate", new { accountId, status, fromDate, toDate });
// Load the references
var employees = dbCon.Select<Employee>();
result.Merge(employees);
return result;
Then it resulted in the error below. I know that I should have removed the merge command. However, it can be fixed by ignoring the Merge command in cases when there is no reference to that table.
{ResponseStatus:{ErrorCode:Exception,Message:Could not find Child Reference for 'Employee' on Parent 'ShelvingCount',StackTrace:"[AllShelvingCounts: 24/06/2015 4:15:01 AM]:
[REQUEST: {AccountId:0,Status:-1,FromDate:2015-06-22,ToDate:2015-06-24}]
System.Exception: Could not find Child Reference for 'Employee' on Parent 'ShelvingCount'
at ServiceStack.OrmLite.OrmLiteUtils.Merge[Parent,Child](List`1 parents, List`1 children)
at Next.Management.Repository.ShelvingCountRepository.<GetAllShelvingCounts>d__0.MoveNext() in c:\dev\Next\Logistics\Management\src\Management.Repository\Repository\ShelvingCountRepository.cs:line 26
Is it some relevant issue to be fixed?
Taking into account that the exception might help the developer to remove the useless merge command, It might be interesting to alert the servicestack developers.
This is working as intended, the error message indicates that it couldn't find a static relationship that could be merged which negates the purpose of the Merge command - to merge related result sets. When there is no statically defined relationship that exists, this is clearly an error the developer should know about since their usage of the API is not working as intended.
This is the same as setting a non-existent/misspelt property in a statically typed language, i.e. the Compiler feedback is there to catch developer errors.

Code First EF delete with Id only and

I have the following code:
public void Remove(Guid id)
{
var symbol = new Symbol()
{
Id = id
};
_ctx.Entry(symbol).State = EntityState.Deleted;
_ctx.SaveChanges();
}
The symbol entity has a related entity called "Category". It is not set up to cascade delete since it is a value from a lookup table. I want to leave the category alone, however when this runs it throws a DbUpdateException:
Entities in 'HourlyContext.Symbols' participate in the 'Symbol_Category' relationship. 0 related 'Symbol_Category_Target' were found. 1 'Symbol_Category_Target' is expected.
I understand that EF is confused about what to do with the relationship, how can I tell it to simply discard the reference to the Category and go ahead and delete the Symbol?
Thanks.
.
EDIT
Okay so I tried making the foreign key nullable (FYI I am using code first Entity Framework) by adding a nullable CategoryId as shown here:
public class Symbol : EntityBase
{
[Required]
public string CompanyName { get; set; }
public int? CategoryId { get; set; }
[Required]
public MarketCategory Category { get; set; }
}
However, after creating a migration and updating the database, this does not resolve the exception.
.
EDIT 2
Okay so it turns out that (looking back now it's obvious) that adding the 'required' attribute prevents the column from being nullable. Now my intention was to not allow the coder (i.e. me) from accidentally setting the relationship to null in code, but allow the framework to null it out when I delete the object via Id. It appears this cannot be done. So all I needed to do was to remove the required attribute (and leave out the 'categoryId' column).
there must be a foreign key relationship being generated by EF. Easy way to check is to use SQL sever managent Studio and check for dependencies on the table
To do so, I think the foreign key column of Category table (SymbolId, whatever ...) Should be Nullable. The OndDelete action of your database table should be set null, too.
Then, EF can simply remove that symbol.

Entity Framework 4 - TPH Inheritance in Features CTP5 (code first) with "IS NULL" discriminator

Hey guys,
I'm trying to create a TPH mapping on a hierarchy where the discriminating clause is the classical "IS NOT NULL" / "IS NULL" case.
Here is the example, database wise:
CREATE TABLE info.EducationTypes
(
ID INT NOT NULL PRIMARY KEY,
Name NVARCHAR(64) NOT NULL,
FKParentID INT NULL REFERENCES info.EducationTypes(ID)
)
the idea is to have a class hierarchy like the following one:
public abstract class EducationType
{
public int ID { get; set; }
public string Name { get; set; }
}
public class MainEducationType : EducationType
{
public IEnumerable<SubEducationType> SubTypes { get; set; }
}
public class SubEducationType : EducationType
{
public MainEducationType MainType { get; set; }
}
I got this schema "working" in the classic xml model, but I really can't find a way to get it working by using the code first approach. This is what I tried...
var educationType = modelBuilder.Entity<EducationType>();
educationType.Map<MainEducationType>(m => m.Requires("FKParentID").HasValue(null));
educationType.Map<SubEducationType>(m => m.Requires("FKParentID"));
Do you have any suggestion?
Unfortunately, having a null value for the discriminator column in TPH mapping is not currently supported in CTP5. This is confirmed by EF team on here and also here. They are looking at it to see if they can make it work for the RTM though.

Resources