我无法让它工作,我使用 data annotationcomputed field 添加了 ServiceStack ormlite Sql server :

[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);
        }
    }

最佳答案

[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,如果您升级到最新版本,它应该可以工作。

关于c# - Servicestack ormlite 错误中的计算字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38494142/

10-12 12:45