NHibernate: INSERT INTO dbo.ActivityPerformed (Date, Quantity, ContractID) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY();@p0 = 2009-12-30 00:00:00 [Type: DateTime (0)], @p1 = 7,5 [Type: Single (0)], @p2 = 3485 [Type: Int32 (0)]
Ah yes, thank you. It wasn't really the primary key column that it exploded upon but a unique index that I hade missed. Thank you and sorry for the noise :) /Christian -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Patrick Steele Sent: den 3 mars 2011 15:03 To: [email protected] Subject: Re: Problems generating identity keys Hmmm... Have you enabled logging to see what types of SQL queries nHibernate is generating? Could shed some light: http://docs.castleproject.org/Active%20Record.Troubleshooting.ashx --- Patrick Steele http://weblogs.asp.net/psteele On Thu, Mar 3, 2011 at 8:55 AM, Christian Axelsson <[email protected]> wrote: > 1. Doesn't work. > > 2. A decent chunk of code below, following the article referenced in my > previous mail: > > public class ActivityPerfomedRepository : > RepositoryBase<ActivityPerformed> > { > // some utility methods will go here > } > > public class RepositoryBase<T> : IRepository<T> where T : > IAggregateRoot > { > public T FindBy(int id) > { > return (T)ActiveRecordMediator.FindByPrimaryKey(typeof(T), > id); > } > > public IList<T> FindAll() > { > return (IList<T>)ActiveRecordMediator.FindAll(typeof(T)); > } > > public virtual void Save(T item) > { > ActiveRecordMediator.Save(item); > } > > public virtual void Delete(T item) > { > ActiveRecordMediator.Delete(item); > } > > public virtual void Update(T item) > { > ActiveRecordMediator.Update(item); > } > > public virtual void Create(T item) > { > ActiveRecordMediator.Create(item); > } > } > > public interface IRepository<T> > { > T FindBy(int id); > IList<T> FindAll(); > void Save(T item); > void Delete(T item); > void Create(T item); > } > > public interface IAggregateRoot > { > } > > /Christian > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Patrick > Steele > Sent: den 3 mars 2011 14:50 > To: [email protected] > Subject: Re: Problems generating identity keys > > 1. Try using [PrimaryKey(Generator = PrimaryKeyType.Identity)] for you > primary key > > 2. If that doesn't work, you'll need to show us what > "_activityPerfomedRepository.Create(activityPerformed);" does. > > --- > Patrick Steele > http://weblogs.asp.net/psteele > > > > On Thu, Mar 3, 2011 at 8:45 AM, Christian Axelsson <[email protected]> wrote: >> Hello, >> >> >> >> I have written a few AR classes using the repository pattern >> suggested at [1]. >> >> For example I have: >> >> >> >> [ActiveRecord(Table = "ActivityPerformed", Schema = "dbo")] >> >> public class ActivityPerformed : IAggregateRoot >> >> { >> >> [PrimaryKey] >> >> public virtual int Id { get; private set; } >> >> >> >> [Property(NotNull = true)] >> >> public virtual DateTime Date { get; set; } >> >> >> >> [BelongsTo(Column = "ContractID", NotNull = true)] >> >> public virtual Contract Contract { get; set; } >> >> >> >> [Property(NotNull = true)] >> >> public virtual float Quantity { get; set; } >> >> } >> >> >> >> However when I try to create a new instance like this: >> >> >> >> var activityPerformed = new ActivityPerformed() >> >> { >> >> Contract = >> activityPerfomViewModel.Contract, >> >> Date = >> activityPerfomViewModel.Date, >> >> Quantity = quantity >> >> }; >> >> >> >> using (new SessionScope(FlushAction.Auto)) >> >> { >> >> _activityPerfomedRepository.Create(activityPerformed); >> >> } >> >> >> >> AR will throw an exception about inserting duplicate row because the >> ID, defaulted to 0, is already exist. >> >> The Id column in the database is an auto incrementing numeric on a >> SQL Server database and I've tried specified both >> PrimaryKeyType.Native and PrimaryKeyType.Identity but I get the same result. >> >> >> >> Any help is appreciated. >> >> >> >> /Christian >> >> >> >> >> >> [1] >> http://fatagnus.com/using-the-repository-pattern-with-activerecord-fr >> o >> m-castle/ >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Castle Project Users" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/castle-project-users?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Castle Project Users" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/castle-project-users?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Castle Project Users" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/castle-project-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.
