I'm trying to create a generic base class for my DataObjects like this :
public abstract class BaseDataObject<TDOType> where TDOType : class
{
public static TDOType Get(Guid lObjectUUID, NpgsqlConnection conn)
{
return conn.Get<TDOType>(lObjectUUID);
}
public Guid Insert(NpgsqlConnection conn)
{
try
{
return conn.Insert<Guid, BaseDataObject<TDOType>>(this);
}
catch (Exception ex)
{
throw;
}
}
}
My inherited class :
[Table("mytable")]
public class MyTable : BaseDataObject<MyTable>
{
[Key]
[Column("mytableuuid")]
public Guid MyTableUUID { get; set; }
[Column("clientuuid")]
public Guid ClientUUID { get; set; }
}
When I call this method from an inherited object, I am thrown with the following message :
42601: syntax error at or near ")"
When I look at the statement in the exception that is being passed, it looks like this :
{insert into "mytable" () values ()}
Even when I cast 'this', as follows, the result is the same :
try
{
BaseDataObject<TDOType> q = this;
return conn.Insert<Guid, BaseDataObject<TDOType>>(q);
}
It would appear that the generic function is not properly seeing the members of my inherited class. What am I doing wrong?
I do not have postgresql database; I used SQL Server. But that should not matter.
Your problem is casting the generic object. Simply call the
conn.Insertlike below:Note how I have cast the parameter to derived class than sending base class in to
conn.Insertmethod. This way, SimpleCRUD can access the members of derived class.In your code, it is not able to see the members of derived class. Hence, it cannot generate the query.
Look at the SQL query generated in your case as you post in question. It does not contain list of columns. Why? Because SimpleCRUD cannot see the properties of derived
MyTableas you are sending in the base classBaseDataObject<TDOType>.I used following database script:
Following is the model declaration:
Following is how I call the
Insertmethod: