使用反射从元数据类中获取属性属性(Using reflection to get property attributes from a metadata class)
我有一个名为Project的LINQ生成类。
我想为生成的属性添加一些属性,因此我使用MetadataType属性定义了一个部分类。
[MetadataType(typeof(Project_HiddenProps))] public partial class Project : IProject { // There are also a few additional properties defined here. } public class Project_HiddenProps { [HiddenColumn] public System.Guid Id { get; set; } // Id is an auto-generated property that I've added a custom attribute to }
后来,我使用反射来尝试获取此属性的属性。
var customAttributes = prop.GetCustomAttributes(false);
但是,我只获得了
System.Data.Linq.Mapping.ColumnAttribute
类型的一个属性。 我的HiddenColumn
属性未包含在集合中。 我的问题是如何使用反射来获取这些元数据属性。任何帮助将不胜感激。
I have a LINQ generated class called Project.
I wanted to add some attributes to the generated properties, so I defined a partial class with the MetadataType attribute.
[MetadataType(typeof(Project_HiddenProps))] public partial class Project : IProject { // There are also a few additional properties defined here. } public class Project_HiddenProps { [HiddenColumn] public System.Guid Id { get; set; } // Id is an auto-generated property that I've added a custom attribute to }
Later, I use reflection to try to get the attributes of this property.
var customAttributes = prop.GetCustomAttributes(false);
I only get one attribute, though, of type
System.Data.Linq.Mapping.ColumnAttribute
. MyHiddenColumn
attribute is not included in the collection. My question is how to get at these metadata properties using reflection.Any help would be greatly appreciated.
原文:https://stackoverflow.com/questions/7742046
最满意答案
好吧,那里你有很多泡菜。
一方面,您需要在表上使用instead of insert
触发器,但另一方面,您希望获得此触发器生成的标识返回到激活它的存储过程。由于无法向触发器发送参数,因此您必须执行以下三种操作之一:
找到一些方法来消除对它的需要而不是触发器。
这是我最好的建议。将存储过程分为两部分:一部分将执行所有操作,直到
insert into
语句(包括它),从而激活instead of insert
触发器,以及另一部分将执行触发后所需的所有操作。 这样,您可以使用scope_identity()
instead of insert
触发器,并将其返回值作为参数发送到第二个存储过程。
注意:此设计意味着您必须逐个插入记录。 如果您尝试插入多条记录,scope_identity()
将仅返回触发器插入的最后一行的标识。- 找到一些在存储过程和而不是触发器之间传递数据的方法。 由于触发器不能除了或返回参数,因此您必须使用临时表或常规表。 建议的解决方案仅作为最后的手段,因为它会使您的代码复杂化,并可能导致一些性能问题。 此外,您必须找到一种方法来执行存储过程,直到触发器完成它的工作。 我可以给你一些关于如何在程序和触发器之间共享数据的指针,但我真的建议不要选择这个解决方案。
Well, you've got yourself quite a pickle there.
From the one hand, you need theinstead of insert
trigger on your table, but from the other hand, you want to get the identity that this trigger generates back to the stored procedure that activated it.Since there is no way to send parameters to and from triggers, you will have to do one of 3 things:
Find some way to eliminate the need for that instead of trigger.
This is my best recommendation.Break your stored procedure to 2 parts: One part will do everything until the
insert into
statement (including it), thus activating theinstead of insert
trigger, and the other part that will do all operations needed after the trigger. this way you can use thescope_identity()
inside theinstead of insert
trigger and send it's return value to the second stored procedure as a parameter.
Note: this design means you have to insert records one by one. should you try to insert more then one record,scope_identity()
will only return the identity of the last row inserted by the trigger.- Find some way of passing data between the stored procedure and the instead of trigger. Since triggers can't except or return parameters, you will have to use either a temporary table or a regular table. This suggested solution is only suggested as a last resort, since it will complicate your code and probably cause some performance issues as well. Also, you will have to find a way to hold execution of the stored procedure until the instead of trigger will finish it's work. I can give you some pointers on how to share data between the procedure and the trigger, but I really suggest not to choose this solution.
相关问答
更多-
对于批量插入,插入触发器被调用一次,但在触发器上,您可以使用特殊的inserted表来获取所有插入的行。 所以,假设你有一个像这样的插入触发器,记录插入到table所有行 create trigger trgInsertTable on dbo.table for insert as insert tableLog(name) select name from inserted 使用此触发器时,当您在table上进行批量插入时, tableLog会填充插入到table的相同行数 对于您特定 ...
-
已知@@identity有缺陷, 因为它根据您会话中执行的最后一次插入提供答案。 如果切换到scope_identity ,就像大多数人已经建议的那样,它将解决问题而不必更改触发器 - 因为触发器定义了自己的范围。 例如,有两个表,T1和T2,并且在T1上定义了INSERT触发器。 当一行插入T1时,触发器将触发并在T2中插入一行。 此方案说明了两个范围:T1上的插入和触发器上T2的插入。 假设T1和T2都有标识列,@@ IDENTITY和SCOPE_IDENTITY在T1上的INSERT语句末尾返回不同的 ...
-
正如您所提到的,SCOPE_IDENTITY是针对这种情况而设计的。 与@@ IDENTITY不同,它不受AFTER触发代码的影响。 除了使用存储过程,这没关系。 我使用AFTER触发器进行审计,因为它们很方便......也就是说,写入触发器中的另一个表。 编辑: SQL Server 2005 cam中的SCOPE_IDENTITY和并行性存在问题 As you mentioned, SCOPE_IDENTITY is designed for this situation. It's not affec ...
-
好吧,那里你有很多泡菜。 一方面,您需要在表上使用instead of insert触发器,但另一方面,您希望获得此触发器生成的标识返回到激活它的存储过程。 由于无法向触发器发送参数,因此您必须执行以下三种操作之一: 找到一些方法来消除对它的需要而不是触发器。 这是我最好的建议。 将存储过程分为两部分:一部分将执行所有操作,直到insert into语句(包括它),从而激活instead of insert触发器,以及另一部分将执行触发后所需的所有操作。 这样,您可以使用scope_identity() i ...
-
插入SQL Server触发器以及如何引用插入的数据(SQL Server trigger on insert and how to reference the data that was inserted)[2022-07-16]
使用' 插入 '表格: CREATE TRIGGER new_trigger_INSERT ON old_table FOR INSERT INSERT INTO new_table (id, first_name, last_name) SELECT col1, col2, col3 FROM inserted [PS:不要忘记确保你的触发器处理多行...] 参考。 创建触发器 好文章: 探索SQL Server触发器 Use the 'inserted' table: CREATE TRIGG ... -
你不能。 从microsoft.com上的使用插入和删除的表文章中,您可以阅读: inserted表在INSERT和UPDATE语句期间存储受影响行的副本。 这意味着inserted表只包含当前INSERT或UPDATE语句的行。 如果您确实希望查看多个此类INSERT或UPDATE语句的所有行,则必须将这些行存储在您自己创建的表中。 You can not. From the Use the inserted and deleted Tables article on microsoft.com, you ...
-
一个问题是国家A和B之间的滞后和联系。 发生远程插入时,本地事务仍在运行。 这会降低性能并增加阻塞。 远程服务器也可能不可用。 或者花太长时间来回应。 您可以将触发器编码为不会失败,但是您的数据不一致。 最后,如果远程服务器上有插入,则会出现多主机问题。 我真的会考虑像复制这样的解决方案将数据发布到另一台服务器。 或者考虑使用服务代理来解耦本地和远程插入。 One problem is the lag and connection between countries A and B. While the r ...
-
INSERT可以在触发器“插入”表中产生多行吗?(Can an INSERT result in more than one row in a trigger “inserted” table?)[2023-04-22]
尝试 insert into TriggerTest (col2) select 'a' union select 'b' union select 'c' try insert into TriggerTest (col2) select 'a' union select 'b' union select 'c' -
如何防止未插入行时触发插入触发器?(How to prevent an Insert Trigger from being fired when no row is inserted?)[2022-01-11]
是的,这是正常的行为。 可以避免,尽管这样做需要3个触发器: 用于将包布尔变量设置为FALSE的BEFORE触发器 FOR EACH ROW触发器在插入行时将变量设置为TRUE 您有AFTER触发器,现在您可以在执行其操作之前检查变量的值。 听起来像矫枉过正? 也许是:试图用你的触发器实现什么? Yes, it is normal behaviour. It can be avoided, though doing so requires having 3 triggers: A BEFORE trigge ... -
你的触发器有一个主要的缺陷,你似乎总是在Inserted表中只有一行 - 事实并非如此,因为每个语句都会调用一次触发器(每行不会调用一次 ),所以如果你一次插入20行,触发器只调用一次 , Inserted伪表包含20行。 因此,代码如下: Select @UserId = user_id, @UserDetail = user_detail + '@' + user_explanation From INSERTED; 将失败 ,因为您将从Inserted表中仅检索一个 (任意) ...