Servicestack ormlite 错误中的计算字段
Computed field in Servicestack ormlite error
我无法让它工作,我使用 ServiceStack ormlite Sql server
:
为 computed field
添加了 data annotation
[Compute, Ignore]
public string FullName { get; set; }
问题是我的 LoadSelect<Employee>()
方法没有从 computed
字段加载 FullName 列。为什么?
如果我删除它加载的 [Ignore]
,但是当我使用 .create()
方法创建新记录时,它 returns 一个错误,可能是因为它试图添加全名字段的值。
Table
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]
员工class:
[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]
属性来处理计算列我刚刚 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,如果您升级到最新版本它应该可以工作。
我无法让它工作,我使用 ServiceStack ormlite Sql server
:
computed field
添加了 data annotation
[Compute, Ignore]
public string FullName { get; set; }
问题是我的 LoadSelect<Employee>()
方法没有从 computed
字段加载 FullName 列。为什么?
如果我删除它加载的 [Ignore]
,但是当我使用 .create()
方法创建新记录时,它 returns 一个错误,可能是因为它试图添加全名字段的值。
Table
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]
员工class:
[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]
属性来处理计算列我刚刚 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,如果您升级到最新版本它应该可以工作。