FluentMigrator-检查数据/行是否存在

时间:2019-08-19 21:00:10

标签: fluent-migrator

我正在使用FluentMigrator将一个数据库模式迁移到另一个数据库模式。我有一种情况,在添加新数据之前,我想检查是否存在某些数据(特别是一行)。

CharacterController controller;


    void Start() {
    controller = player.GetComponent<CharacterController>();
    }  
    void Update()
    {
        if(hitted1){
            posi = new Vector3(42.49f, 0.5f, 163.8f);
            controller.enabled = false;
            player.transform.position = posi;
            controller.enabled = true;
            hitted1 = false;
        }
    }

如何检查该行首先存在?

1 个答案:

答案 0 :(得分:2)

从3.0版开始,FluentMigrator中没有内置功能可插入不存在的行。 GitHub上有添加此功能的请求:https://github.com/fluentmigrator/fluentmigrator/issues/640

但是,您可以使用Execute.Sql()方法并编写自己的SQL查询,以在插入行之前检查该行是否存在,如此处Check if a row exists, otherwise insert所示。

Execute.Sql(@"
begin tran

if not exists (select * from MyTable with (updlock, rowlock, holdlock) where id='100' and Field='Value')
begin
    insert into MyTable values (100, 'Value')
end

commit
");
相关问题