Is this possible... iirc, it was overwriting the assigned pkey in the new
object... ie: have the generator check the value, if it isn't (new Guid())
(all 0 bits), then generate, otherwise leave it...
If I could get an example of a custom generator I'd be happy to use that...
--
Michael J. Ryan - tracker1(at)theroughnecks(dot)com - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)
... TODO: re-create tagline file... stupid reinstall...
Michael this is how I have our projects setup. Seems to work well for us.
public abstract class EntityGuidBase<T> : ActiveRecordValidationBase<T>, IGuidEntity where T : class
{
private Guid? _id = null;
[PrimaryKey(Generator = PrimaryKeyType.Assigned)]
public virtual Guid? Id
{
get { return _id; }
set { _id = value; }
}
public override void Save()
{
if (_id.HasValue)
UpdateAndFlush();
else
{
_id = Guid.NewGuid();
CreateAndFlush();
}
}
…..
<br
Wow - clever!
j.
On 11/5/2007 4:28 PM, Shawn Carr wrote:
> Michael this is how I have our projects setup. Seems to work well for us.
>
>
>
> public abstract class EntityGuidBase<T> : ActiveRecordValidationBase<T>,
> IGuidEntity where T : class
>
> {
>
> private Guid? _id = null;
>
>
>
> [PrimaryKey(Generator = PrimaryKeyType.Assigned)]
>
> public virtual Guid? Id
>
> {
>
> get { return _id; }
>
> set { _id = value; }
>
> }
>
>
>
> public override void Save()
>
> {
>
> if (_id.HasValue)
>
> UpdateAndFlush();
>
> else
>
> {
>
> _id = Guid.NewGuid();
>
> CreateAndFlush();
>
> }
>
> }
>
> …..
>
> }
>
>
>
> *From:* castle-pro...@googlegroups.com
> [mailto:castle-pro...@googlegroups.com] *On Behalf Of *Ayende Rahien
> *Sent:* Monday, November 05, 2007 6:22 PM
> *To:* castle-pro...@googlegroups.com
> *Subject:* Re: ActiveRecord with pre-assigned PKeys..
>
>
>
> Possible, you need to use Create and Update explicitly, rather than Save()
>
> On 11/6/07, *Michael J. Ryan* < tracker...@theroughnecks.com
> <mailto:tracker...@theroughnecks.com>> wrote:
>
>
> I am using ActiveRecord in a project where some data will be replicated
> from
> one site to another... I'd *LIKE* to be able to use the same classes to
> input
> the data from a foreign site into the local dbms... I've been using the
> Guid/GuidComb generators, but would like to not overwrite the PKey(GUID) if
> it's already assigned...
>
> Is this possible... iirc, it was overwriting the assigned pkey in the new
> object... ie: have the generator check the value, if it isn't (new Guid())
> (all 0 bits), then generate, otherwise leave it...
>
> If I could get an example of a custom generator I'd be happy to use that...
>
> --
> Michael J. Ryan - tracker1(at)theroughnecks(dot)com -
> www.theroughnecks.net <http://www.theroughnecks.net>
Shawn's code will throw if you call Save when the entity is not
persisted in the database. (it will do an UPDATE - find 0 rows
affected and throw). You will need to come up with a convention that
will manage this your self. isNew might work depending on how it's
implemented. How will you know if it's new or already imported?
The key thing is that if the row already exists then you need to call
Update and if it's new you need to call Create.
And yes - he is creating his classes like:
[ActiveRecord]
public class MyEntity : EntityGuidBase<MyEntity> {}
j.
public override void Save()
{
if (_id.HasValue)
UpdateAndFlush();
else
{
_id = Guid.NewGuid();
CreateAndFlush();
}
}
The key part of this method is the _id.HasValue. This is the isNew
functionality that you are referring to because when a new entity is created
its id is null until the save which at this point it figures this out and
assigns a new Guid to it. When it is loaded via the db the ID will have a
value so it know to do an update.
Whatever method you use you need some sort of isNew functionality so you can
call update or create directly.