0

トリガーの問題を解決したいと思っていました。すべてのドキュメントによると、C# で (パターン リポジトリ経由で) 挿入またはアップグレードを行う場合、トリガーは値を返す必要があります。これは、単純なトリガーに対してはうまく機能します。

より複雑なトリガーを使用すると、戻り値でエラーが発生します。

a) トリガーSELECT @@identity から戻る場合。

null を許可しない値の型である System.Int32 型のメンバー。Null に値を代入することはできません。

b) トリガーから戻る場合: select * from dbo.Table where IDColumn = scope_identity();

Operation failed AutoSync member. In order to perform after insertion members operation
AutoSynced must have a type either automatically generated identity, or a key that does
not change the database after the insertion.

使用: C#、.NET FW4.5、Linq to SQL、MSSQL Express 2012 (互換性 2008)

トリガーと SQL へのリンクに関する問題への指示と同様に、私は喜んでいます。

より複雑なトリガー (クリーニングして最小限に抑えたもの):

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[bitrgTable] 
   ON [dbo].[Table]
   INSTEAD OF INSERT 
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT OFF;        

    -- Declare variables
    DECLARE @new...

    -- Get inserted values
    SELECT @new... FROM INSERTED  

    -- do update all unique values
    UPDATE [dbo].[Table] SET
         ...
    WHERE 
        ...;

    -- if the last result is null
    IF @@ROWCOUNT = 0 
    BEGIN
        -- Do insert new values
        INSERT INTO [dbo].[Table]
            (...)
        SELECT 
             ...
        FROM INSERTED
    END

    -- somehow return the result
    -- ----------------------------------------------------------------
    -- select * from dbo.table where TableID = scope_identity();
    -- SELECT @@identity;   
    SELECT ??????
END

よろしく、ピーター

Ismet Alkan 補完質問の回答に基づいています。

// Reposity class
public class Reposity
{
  private DataContext myDataContext = new DataContext();

  public void Save()
  {
    myDataContext.SubmitChanges();
  }
}

// Where working with reposity (class in console app)
public class ConsoleShell
{
   // declare variable
   Reposity myReposity = new Reposity();

  // Many methods, working with treads etc,

  // here I worked with Save()
  private void ExecuteThread(....)
  {
    ...

    // go thŕow all found results
    foreach (KeyValuePair<Oid, AsnType> kvpDot1dTpFdbAddress in dictDot1dTpFdbAddress)
    {
      // create new object instance
      CustomObjectFromLinq objCustomObjectFromLinq = new CustomObjectFromLinq();

      // append values for objCustomObjectFromLinq

      if(CONDITION == TRUE)
      {
        // save item
        myReposity.Add(objCustomObjectFromLinq);
        myReposity.Save();
      }
      else
      {
        // if want ignore result, do nothing, prevent object null
        objCustomObjectFromLinq = null;
      }

      .. next codes

    } -- end foreach
  } -- end method
} -- end class
4

1 に答える 1

0

私はあなたが言及した方法を試しました。ただし、これには多大な労力が必要であり、実際にこれを回避する必要はありません。ここで正確な答えを出しているわけではありませんが、私がそれを機能させた方法です。

DataContextを属性として持つクラスを作成しています。それを継承するクラスを作った方がいいと思います。DbContextの代わりに ASP.NET MVC3 アプリケーションで使用しましDataContextたが、大きな違いがあるかどうかはわかりません。もう 1 つは、要素をコンテキストに直接追加するのではなく、DBSetコンテキスト内の a に追加することです。これが私のコンテキストクラスです。

public class YourDBContext : DbContext
{
    public DbSet<YourObject> YourObjects { get; set; }
}

コンテキストの操作:

public class TheClassYouOperate
{
    private YourDBContext yourDBContext = new YourDBContext();

    public int SaveAndReturnID(YourObject newObject)
    {
        if (ModelState.IsValid) // or CONDITION == TRUE for your case
        {
            yourDBContext.YourObjects.Add(newObject);
            yourDBContext.SaveChanges();
            return newObject.ID;
        }
        return 0; // or null or -1, whatever the way you'll understand insertion fails
    }
}

それが役に立てば幸い!

于 2013-03-24T19:13:36.577 に答える