简体   繁体   中英

OrmLite Inserting 0 and instead of auto-incrementing primary key

I am trying to create a generic Insert<T> for our objects. I am new to OrmLite so I am still reading up on it. The objects that are used do not use an Id property they have a more detailed name.

As an example this a basic POCO:

public class Customer
{
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...etc.
} 

So the primary key is CustomerId and through some more reading I found that OrmLite likes to use the property Id for the primary keys. As we have a convention not to use just the name Id for the FK I cannot switch. However reading further it seemed like I could decorate the property with an attribute or two and get it to work.

This is what I am working with:

public class Customer
{
    [AutoIncrement]
    [Alias("CustomerId")]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...
}

I get a SqlException stating the following:

Additional information: Cannot insert the value NULL into column 'CustomerId', table 'XXX.dbo.Customer'; column does not allow nulls. INSERT fails

I did some more reading and thought I could fix the issue by inheriting from an interface.

public class Customer : IHasId<int>
{
    [AutoIncrement]
    [Alias("CustomerId")]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    ...
}

I have played with using the PrimaryKey attribute and I still get the same result.

Has anyone had an issue like this? If you did how did you solve it? I am having a hard time finding some more information on the matter.

I can get rid of the attributes and name the property back to CustomerId so it matches the db table and it will insert into the table but it will always put in 0 as the key, which makes sense just because it is the default value for the int but does not help me when it has to be an autoincrementing primary key. As a side note I am using ServiceStack.OrmLite.SqlServer.3.9.71 and SQL Server 2008

UPDATE 1

So I went through the documentation again today for 3.9 version of ServiceStack.OrmLite and read through their description on what I should do when I don't have POCOs with an 'Id' property for the Primary Key. It is as follows:

... by convention OrmLite expects it to be Id although you can use [Alais("DbFieldName")] attribute to map it to a column with a different name or use the [PrimaryKey] attribute to tell OrmLite to use a different property for the primary key.

I used both of the examples and it does in fact insert my data to the SQLDatabase. However, it is still inserting 0 for the CustomerId primary key.

If I use the AutoIncrement attribute it throws a SqlException:

An exception of type 'System.Data.SqlClient.SqlException' occured in System.Data.dll but was not handled by user code. Additional Information: Cannot insert the value NULL into column 'CustomerId', table 'dbo.Customer'; column does not allow nulls. INSERT fails.

Has anyone run into this issue? I keep running into roadblocks.

i experimented the same issue. Your following code was already good.

public class Customer
    {
        [AutoIncrement]
        [Alias("CustomerId")]
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    ...
    }

The problem don't come from ORMLITE but from your database. Indeed, the column "CustomerId" which is i think the primary key for your table have his property "Identity" set to "False". You must set it to "True" or "Yes" and also set "Identity Increment" and "Identity Seed" to 1.

In v4.0.40, servicestack retrieves the primary key column by naming convention ("column_name" == OrmLiteConfig.IdField) as shown by the following code from OrmLiteConfigExtensions.cs:

    internal static bool CheckForIdField(IEnumerable<PropertyInfo> objProperties)
    {
        // Not using Linq.Where() and manually iterating through objProperties just to avoid dependencies on System.Xml??
        foreach (var objProperty in objProperties)
        {
            if (objProperty.Name != OrmLiteConfig.IdField) continue;
            return true;
        }
        return false;
    }

Therefore, using [AutoIncrement] with [Alias] should not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM