psulek wrote:
Hi,
Anybody using composite primary keys with DataObjects.NET? I am unable to find sample for how to create class with composite primary keys.
If you have experience or idea, please let me know.
Cheers!
Alex Ko Aung
Have you tried mark properties with attribute [Key] and its property "Position" ? (like [Key(Position = 0)].
Sample:
class MyEntity
{
[Key(Position=0)]
public int Key1 {get;set;}
[Key(Position=1)]
public int Key2 {get;set;}
}
I i understand manual than this is how to make composite primary keys in DO4.
Alex (Xtensive) wrote:
Also note that Entity.Key describes any key type. I.e. you can use this code also for entities with composite keys:
var key = entityWithCompositeKey.Key;
Console.WriteLine("Key: {0}", key); // We almost always implement ToString(); [DebuggerDisplay] is also frequently applied
// Key can be serialized to string
var keyAsString = key.Format();
var deserializedKey = Key.Parse(keyAsString); // Relies on Domain.Demand(), see the description of this method
Assert.AreEqual(key, deserializedKey);
// Resolving entity by its key
var theSameEntity = Qyery.Single(deserializedKey);
Assert.AreSame(entityWithCompositeKey, theSameEntity);
Alex (Xtensive) wrote:
Few more examples:
// Getting entity with composite key
var entity1 = Query.Single<MyEntity>(1,2,"X");
// The same code, but by explicitly creating composite key
var key = Key.Create<MyEntity>(1,2,"X");
var entity2 = Query.Single<MyEntity>(key);
psulek wrote:
Peter, thanks again :)
You can also write simply [Key(1)] (there is a constructor accepting Position argument).
You're welcome, Alex.
Btw i could not find how to build composite primary keys in latest manual (checked only PDF version which is installed with DO4.3 RC4).
Alex (Xtensive) wrote:
Two facts:
-
DO4 doesn't allow you to change entity key after the construction of entity.
-
You can explicitly specify entity key on creation using appropriate Entity constructor: Entity..ctor(params object[] keyComponents).
If you need to change the key of already created entity, the only way to do this is to remove and create it again - with the new key.
Alex (Xtensive) wrote:
Err... This isn't related to database at all.
You should:
So final code must look like:
[Serializable]
[HierarchyRoot]
public class WebPage : Entity
{
[Key(Position=0), Field]
public int Id { get; private set; }
[Key(Position=1), Field]
public string PageKey { get; private set; }
[Field(Length = 200)]
public string Title { get; set; }
[Field(Length = 200)]
public string Url { get; set; }
public WebPage(int id, string pageKey)
: base(id, pageKey) // this call sets Key explicitly
{
// ... Key is already set here
}
}
Btw, as I suspect, a part of key (Id) must be assigned automatically in this case. So the solution here is:
[Serializable]
[HierarchyRoot]
internal class IntKeyed : Entity
{
[Key, Field]
public int Id { get; private set; }
}
[Serializable]
[HierarchyRoot]
public class WebPage : Entity
{
[Key(Position=0), Field]
public int Id { get; private set; }
[Key(Position=1), Field]
public string PageKey { get; private set; }
[Field(Length = 200)]
public string Title { get; set; }
[Field(Length = 200)]
public string Url { get; set; }
public WebPage(string pageKey)
: base(Key.Create<IntKeyed>().Value.GetValue<int>(0), pageKey) // this call sets Key explicitly
{
// ... Key is already set here
}
}
answered
Jul 02 '10 at 06:14
Editor
46●156●156●157