Asi quedo el mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MRGest.Domain" namespace="MRGest.Domain">
<class name="Brand" table="Brand">
<id name="Id" column="Id" type="int">
<generator class="identity"/>
</id>
<property name="Name" />
</class>
</hibernate-mapping>
Este es el codigo del repository:
public void Add(TEntity entity)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction =
session.BeginTransaction())
{
session.Save(entity);
transaction.Commit();
}
}
Y De esta manera llamo al metodo Add:
MRGest.Domain.Brand brand = new MRGest.Domain.Brand();
brand.Name = name;
MRGest.Data.Repositories.BrandRepository repository =
new MRGest.Data.Repositories.BrandRepository();
repository.Add(brand);
Esto es lo que muestra el profiler:
exec sp_executesql N'INSERT INTO Brand (Name, Id) VALUES (@p0,
@p1)',N'@p0 nvarchar(4000),@p1 int',@p0=N'Test',@p1=0
De todas maneras, sigue arrojando el mismo error.
Gracias!!
On Mar 19, 3:23 pm, José F. Romaniello <[email protected]> wrote:
> Aparentemente tenes todo bien :(
> 1-proba sacarle unsaved-value="0"
> 2-mostranos como haces para insertar un nuevo brand
>
> saludos,
>
> 2011/3/19 Ezequiel Calderara <[email protected]>
>
> > Si, este esl hibernate.cfg.xml
>
> > <?xml version="1.0" encoding="utf-8" ?>
> > <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
> > <session-factory>
> > <property
> > name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
> > property>
> > <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</
> > property>
> > <property
> > name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
> > property>
> > <property name="connection.connection_string">Data
> > Source=localhost;Initial Catalog=MRGest;Integrated
> > Security=True;Application Name=HibernateConnection</property>
> > <property
>
> > name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> > NHibernate.ByteCode.Castle</property>
> > <property name="show_sql">true</property>
> > </session-factory>
> > </hibernate-configuration>
>
> > On 19 mar, 13:03, José F. Romaniello <[email protected]> wrote:
> > > podrías mostrar como estas configurando nhibernate?
> > > que dialect estas usando?
>
> > > 2011/3/19 Ezequiel Calderara <[email protected]>
>
> > > > Buenas a todos, soy nuevo en el grupo y nuevo con nHibernate.
> > > > Estuve buscando codigo de ejemplo e informacion sobre los generators
> > > > para ver que es lo que estoy haciendo mal.
>
> > > > Tengo la clase Brand, que se mapea con la Tabla Brand, la cual tiene
> > > > el campo id un int con identity(1,1) (MS SQL Server).
> > > > Segun leí debo utilizar un generator con la clase "identity" o
> > > > "native". Probé con ambos con los mismos resultados.
>
> > > > El caso mas simple es, tengo esta clase Brand:
> > > > =========================================
> > > > public class Brand
> > > > {
>
> > > > private int _id;
> > > > private string _name;
>
> > > > public Brand()
> > > > {
>
> > > > }
>
> > > > public virtual int Id
> > > > {
> > > > get { return _id; }
> > > > set { _id = value; }
> > > > }
>
> > > > public virtual string Name
> > > > {
> > > > get { return _name; }
> > > > set { _name = value; }
> > > > }
>
> > > > }//end Brand
> > > > =========================================
>
> > > > Que estoy mapeando contra esta tabla:
> > > > =========================================
> > > > CREATE TABLE [dbo].[Brand](
> > > > [id] [int] IDENTITY(1,1) NOT NULL,
> > > > [Name] [varchar](50) COLLATE Traditional_Spanish_CI_AS NOT NULL,
> > > > CONSTRAINT [PK_Brand] PRIMARY KEY CLUSTERED
> > > > (
> > > > [id] ASC
> > > > )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
> > > > = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
> > > > ) ON [PRIMARY]
> > > > =========================================
>
> > > > Y Este es el archivo de mapping:
> > > > =========================================
> > > > <?xml version="1.0" encoding="utf-8" ?>
> > > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> > > > assembly="MRGest.Domain" namespace="MRGest.Domain">
>
> > > > <class name="Brand" table="Brand">
> > > > <id name="Id" column="Id" type="int" unsaved-value="0">
> > > > <generator class="native"/>
> > > > </id>
> > > > <property name="Name" />
> > > > </class>
> > > > </hibernate-mapping>
> > > > =========================================
> > > > Y al momento de hacer el insert, me arroja esta excepcion:
> > > > =========================================
> > > > NHibernate.Exceptions.GenericADOException: could not insert:
> > > > [MRGest.Domain.Brand#0][SQL: INSERT INTO Brand (Name, Id) VALUES
> > > > (?, ?)] ---> System.Data.SqlClient.SqlException: Cannot insert
> > > > explicit value for identity column in table 'Brand' when
> > > > IDENTITY_INSERT is set to OFF.
> > > > en System.Data.SqlClient.SqlConnection.OnError(SqlException
> > > > exception, Boolean breakConnection)
> > > > en System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
> > > > exception, Boolean breakConnection)
> > > > en
>
> > System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
> > > > stateObj)
> > > > en System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
> > > > SqlCommand cmdHandler, SqlDataReader dataStream,
> > > > BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
> > > > stateObj)
> > > > en
> > > > System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
> > > > RunBehavior runBehavior, String resetOptionsString)
> > > > en
> > > > System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
> > > > cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean
> > > > async)
> > > > en
> > > > System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
> > > > cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
> > > > method, DbAsyncResult result)
> > > > en
> > > > System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
> > > > result, String methodName, Boolean sendToPipe)
> > > > en System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
> > > > en NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand
> > > > cmd) en d:\CSharp\NH\nhibernate\src\NHibernate\AdoNet
> > > > \AbstractBatcher.cs:línea 216
> > > > en NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation
> > > > expectation) en d:\CSharp\NH\nhibernate\src\NHibernate\AdoNet
> > > > \NonBatchingBatcher.cs:línea 39
> > > > en
> > > > NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,
> > > > Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql,
> > > > Object obj, ISessionImplementor session) en d:\CSharp\NH\nhibernate\src
> > > > \NHibernate\Persister\Entity\AbstractEntityPersister.cs:línea 2643
> > > > --- Fin del seguimiento de la pila de la excepción interna ---
> > > > en
> > > > NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,
> > > > Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql,
> > > > Object obj, ISessionImplementor session) en d:\CSharp\NH\nhibernate\src
> > > > \NHibernate\Persister\Entity\AbstractEntityPersister.cs:línea 2663
> > > > en
> > > > NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id,
> > > > Object[] fields, Object obj, ISessionImplementor session) en d:\CSharp
> > > > \NH\nhibernate\src\NHibernate\Persister\Entity
> > > > \AbstractEntityPersister.cs:línea 3015
> > > > en NHibernate.Action.EntityInsertAction.Execute() en d:\CSharp\NH
> > > > \nhibernate\src\NHibernate\Action\EntityInsertAction.cs:línea 59
> > > > en NHibernate.Engine.ActionQueue.Execute(IExecutable executable) en
> > > > d:\CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:línea 136
> > > > en NHibernate.Engine.ActionQueue.ExecuteActions(IList list) en d:
> > > > \CSharp\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs:línea 126
> > > > en NHibernate.Engine.ActionQueue.ExecuteActions() en d:\CSharp\NH
> > > > \nhibernate\src\NHibernate\Engine\ActionQueue.cs:línea 169
> > > > en
>
> > NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource
> > > > session) en d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default
> > > > \AbstractFlushingEventListener.cs:línea 249
> > > > en
> > > > NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent
> > > > event) en d:\CSharp\NH\nhibernate\src\NHibernate\Event\Default
> > > > \DefaultFlushEventListener.cs:línea 19
> > > > en NHibernate.Impl.SessionImpl.Flush() en d:\CSharp\NH\nhibernate
> > > > \src\NHibernate\Impl\SessionImpl.cs:línea 1479
> > > > en NHibernate.Transaction.AdoTransaction.Commit() en d:\CSharp\NH
> > > > \nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:línea 187
> > > > en MRGest.Data.Repositories.BaseRepository`2.Add(TEntity entity) en
> > > > D:\Proyectos\Ironicnet\ironicnet.com\MRGest\trunk\src\MRGest\Data
> > > > \Repositories\BaseRepository.cs:línea 18
> > > > en
>
> > Web.Controllers.Modules.Stock.ProductBrand.ProductBrandCRUD.Create(String
> > > > name, String description) en D:\Proyectos\Ironicnet\ironicnet.com
> > > > \MRGest\trunk\src\MRGest\Web\Controllers\Modules\Stock\ProductBrand
> > > > \CRUD.aspx.cs:línea 19
> > > > =========================================
>
> > > > Muchas gracias, saludos.
>
> > > > --
> > > > Para escribir al Grupo, hágalo a esta dirección:
> > > > [email protected]
> > > > Para más, visite:http://groups.google.com/group/NHibernate-Hispano
>
> > --
> > Para escribir al Grupo, hágalo a esta dirección:
> > [email protected]
> > Para más, visite:http://groups.google.com/group/NHibernate-Hispano
--
Para escribir al Grupo, hágalo a esta dirección:
[email protected]
Para más, visite: http://groups.google.com/group/NHibernate-Hispano