Servicestack或lite错误中的计算字段
本文关键字:计算 字段 错误 lite Servicestack | 更新日期: 2023-09-27 17:57:52
我无法使其工作,我使用ServiceStack ormlite Sql server
:为computed field
添加了data annotation
[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);
}
}
[Ignore]
属性告诉OrmLite你希望它完全忽略属性,这不是你想要的,你只需要使用[Compute]
属性来处理计算列,我刚刚在这个提交中添加了一个测试,该提交在最新版本的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,如果你升级到最新版本,它应该可以工作。