sql-serverasp.net-mvcinsertorchardcmsirepository

How to use IRepository in Orchard with int key (NOT IDENTITY)


I'm using Orchard IRepository to manage data and save to db... that's what I'm trying to do.

My class is:

public class SocietaRecord
{
    public virtual int Id { get; set; }
    public virtual string Club { get; set; }
    public virtual string Email { get; set; }
}

My Migration:

SchemaBuilder.CreateTable("SocietaRecord", table => table
    .Column<int>("Id", column => column.PrimaryKey())
    .Column<String>("Club", c => c.Nullable())
    .Column<String>("Email", c => c.Nullable())
);

Please note that Id is NOT and IDENTITY

now when I call Repository Create the follow insert is performed: (grabbed with sql profiler)

exec sp_executesql N'INSERT INTO Match_SocietaRecord 
(Club, Email) VALUES (@p0, @p1); 
select SCOPE_IDENTITY()',N'@p0 nvarchar(4000),@p1 nvarchar(4000),
@p0=N'Test',@p1=NULL

that fails because it ignores my Id and assumes (incorrectly) that Tables has an autonumber key.

This is the error:

NHibernate.Exceptions.GenericADOException: could not insert:
[Match.Models.SocietaRecord]
[SQL: INSERT INTO Match_SocietaRecord (Club, Email) VALUES (?, ?); 
select SCOPE_IDENTITY()] ---> System.Data.SqlClient.SqlException: 
Cannot insert the value NULL into column 'Id', table ORCHARD.dbo.Match_SocietaRecord'; 
column does not allow nulls. INSERT fails.

How explain to repository to look at table and do not assume identity if there is not so that I can insert my own id? (Yes, I provided a valid Id on object record before creating)

Edit: I have digged a bit in code and I think that:

so I can change my question in:

How can I define a custom IAutoMappingAlteration for my class to explain Orchard that my Id is not an identity?

or (in other words)

where I can find the "automapping configuration" in orchard as described in Auto-mapping documentation?

https://github.com/jagregory/fluent-nhibernate/wiki/Auto-mapping

(... "You can modify the way the automapper discovers identities by overriding the IsId method in your automapping configuration." ... )


Solution

  • The repository implementation in Orchard is there to be used by higher-level content management API. It is not designed or intended as a general purpose repository implementation or a substitute for an ORM (we have nHibernate under the hoods for that). You should use nHibernate directly. Low-level examples of that in the OrchardPo module: https://bitbucket.org/bleroy/orchardpo/src