This is a migrated thread and some comments may be shown as answers.

Attributes only approach and Guid Identities

1 Answer 45 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jason Reimer
Top achievements
Rank 1
Jason Reimer asked on 06 May 2010, 05:25 PM
I am attempting to use an attributes-only approach for configuring my persistent classes, however, I am unable to figure out how to have the primary key auto-assigned using the Guid generator.  I have a test class like so:

[

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.

1 Answer, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 12 May 2010, 08:32 AM
Hello Jason Reimer,

 At the first try, we were not able to reproduce the exception that you reported. Can you tell us a bit more about the way in which you are creating your model? Has it been generated by the OpenAccess Visual Entity Designer, or did you write your class by hand? Is your database an existing one, or was it created by OpenAccess? We are looking forward to further collaborating on this one so we can get to the bottom of the problem and provide a solution as soon as possible.

All the best,
Zoran
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Development (API, general questions)
Asked by
Jason Reimer
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Share this question
or