【发布时间】:2016-07-21 02:59:06
【问题描述】:
我无法让它工作,我使用ServiceStack ormlite Sql server为computed field添加了data annotation:
[Compute, Ignore]
public string FullName { get; set; }
问题是我的LoadSelect<Employee>() 方法没有从computed 字段加载列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);
}
}
【问题讨论】:
-
您能否提供更多有关使用
.create()的详细信息?这是使用Db.Insert<T>在下面还是其他的自定义包装器?另外,如果您可以为LoadSelect提供完整的示例 POCO,以便其他人可以重现问题? -
是的,这些信息有帮助吗? @Layoric
-
@RogerOliveira 不,它仍然无法运行,它缺少
EmployeeTable 的定义,您是否还可以提供您正在使用的具有 Computed 列的 Employee 的 CREATE TABLE 定义,以便我们对其进行测试本地? -
@mythz 我删除了一些子表字段并更新了这篇文章中的信息。
标签: c# servicestack ormlite-servicestack