Servicestack ormlite错误中的计算字段

时间:2016-07-21 02:59:06

标签: c# servicestack ormlite-servicestack

我无法使其发挥作用,我使用data annotationcomputed field添加了ServiceStack ormlite Sql server

[Compute, Ignore]
public string FullName { get; set; }

问题是我的LoadSelect<Employee>()方法无法从computed字段加载colunm FullName。为什么呢?

如果我删除它加载的[Ignore],但是当我使用.create()方法创建新记录时,它会返回一个错误,可能是因为它试图为FullName字段添加一个值

表格

CREATE TABLE [dbo].[Employee](
    [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
    [FullName]  AS (concat(ltrim(rtrim([FirstName])),' ',ltrim(rtrim([LastName])))) PERSISTED NOT NULL,
    [FirstName] [nvarchar](55) NOT NULL,
    [LastName] [nvarchar](55) NULL,
    [Username] [nvarchar](55) NOT NULL,
    [Password] [nvarchar](55) NULL
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
(
    [EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

员工类:

[Schema("dbo")]
[Alias("Employee")]
public class Employee : IHasId<int>
{
    [PrimaryKey]
    [Alias("EmployeeId")]
    [AutoIncrement]
    [Index(Unique = true)]
    public int Id { get; set;}

    [Required]
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Required]
    [Index(true)]
    public string Username { get; set; }

    public string Password { get; set; }

    [Compute, Ignore]
    public string FullName { get; set; }
}

获取方法:

    public virtual async Task<IEnumerable<T>> Get<T>() where T : IHasId<int>
    {
        using (var dbCon = DbConnectionFactory.OpenDbConnection())
        {
            return await dbCon.LoadSelectAsync<T>(x => x);
        }
    }

创建方法:

    public virtual async Task<T> Create<T>(T obj) where T: IHasId<int>
    {
        using (var dbCon = DbConnectionFactory.OpenDbConnection())
        {
            // if there is an id then INSERTS otherwise UPDATES
            var id = obj.GetId().SafeToLong();

            if (id > 0)
                dbCon.Update(obj);
            else
                id = dbCon.Insert(obj, true);   

            // returns the object inserted or updated
            return await dbCon.LoadSingleByIdAsync<T>(id);
        }
    }

1 个答案:

答案 0 :(得分:3)

[Ignore]属性告诉OrmLite您希望它完全忽略该属性而不是您想要的属性,您需要使用[Compute]属性来处理计算列,我只是added a test for in this commit在OrmLite的最新版本中按预期工作,例如:

db.DropTable<Employee>();
db.ExecuteSql(@"
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[FullName]  AS (concat(ltrim(rtrim([FirstName])),' ',ltrim(rtrim([LastName])))) PERSISTED NOT NULL,
[FirstName] [nvarchar](55) NOT NULL,
[LastName] [nvarchar](55) NULL,
[Username] [nvarchar](55) NOT NULL,
[Password] [nvarchar](55) NULL
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([EmployeeId] ASC)");

var item = new Employee
{
    FirstName = "FirstName",
    LastName = "LastName",
    Username = "Username",
    Password = "Password",
    FullName = "Should be ignored",
};

var id = db.Insert(item, selectIdentity: true);

var row = db.LoadSingleById<ComputeTest>(id);

Assert.That(row.FirstName, Is.EqualTo("FirstName"));
Assert.That(row.FullName, Is.EqualTo("FirstName LastName"));

row.LastName = "Updated LastName";
db.Update(row);

row = db.LoadSingleById<ComputeTest>(id);

Assert.That(row.FirstName, Is.EqualTo("FirstName"));
Assert.That(row.FullName, Is.EqualTo("FirstName Updated LastName"));

使用Create()辅助方法中的异步API也可以使用,例如:

var row = await Create(item);

Assert.That(row.FirstName, Is.EqualTo("FirstName"));
Assert.That(row.FullName, Is.EqualTo("FirstName LastName"));

row.LastName = "Updated LastName";
row = await Create(row);

Assert.That(row.FirstName, Is.EqualTo("FirstName"));
Assert.That(row.FullName, Is.EqualTo("FirstName Updated LastName"));

我假设您使用旧版本的OrmLite,如果您升级到最新版本,它应该有效。