[
Persistent]
[
Table("TestObject")]
[
Serializable]
public class TestObject
{
private Guid _id = Guid.Empty;
[
Column("ID", OpenAccessType = OpenAccessType.Guid, IsPrimaryKey = true, SqlType = "uniqueidentifier", IsBackendCalculated = true)]
[
Storage("_id")]
public Guid ID
{
get { return _id; }
}
[
Column("Name")]
public string Name
{
get;
set;
}
}
And I create the object scope like so:
metadataSource = AttributesMetadataSource.FromAssembly(Assembly.GetExecutingAssembly());
container = metadataSource.GetModel();
container.PersistentTypes.ForEach(x => x.KeyGeneratorName =
KeyGenerator.Guid);
database =
Database.Get("{connection string}", new BackendConfiguration(), container);
return database.GetObjectScope();
This works when I save the objects. It takes the Empty Guid and replaces it with a new one, and that value is saved to the database. However, this seems to cause problems down the road when adding the object to the ObjectContainer because I get the following exception when executing a CopyFrom():
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Telerik.OpenAccess"
StackTrace:
at Telerik.OpenAccess.RT.DisconnectedOID..ctor(Object x)
at OpenAccessRuntime.DataObjects.PMProxy.disconnectedOID(Object oid)
at Telerik.OpenAccess.ObjectContainer.FillFrom(IEnumerator roots, IPersistenceManager from, IEnumerator allobjects, String listName)
at Telerik.OpenAccess.ObjectContainer.CopyFrom(IObjectScope objectScope, String listName, Object objectToBeAdded, IObjectCollector objectCollector)
at OpenAccessTests.Program.UseContainer() in C:\Projects\OpenAccessTests\ConsoleApplication1\Program.cs:line 136
at OpenAccessTests.Program.Main(String[] args) in C:\Projects\OpenAccessTests\ConsoleApplication1\Program.cs:line 38
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Through Reflector, I have traced this all the way back to the PCStateMan.getObjectId(PersistenceCapable pcParam) method returning null, which then is returned from the TestObject's OpenAccessEnhancedGetObjectId() method, which then is then passed to Telerik.OpenAccess.RT.DisconnectedOID..ctor(Object x).
Basically, if there is an easy way to do this, please let me know.