使用 Linq 进行插入,省略一些“假”内容列
我在数据库中有一个表,其中包含以下列:ID、Name、Txt
。我们使用 Linq To Sql 来实现我们的 DAL。另一位同事添加了两列额外的列,因此在代码中会产生相同的表结果:ID、Name、Txt、NameTemp、TxtTemp
。
这两个“假”表用于 LINQ 连接中代码的不同部分,并使用 SQL Profiler 进行分析,解析的 SQL 查询采用“真实”列,并且一切正常。 现在我需要使用该表进行 INSERT,但出现异常,因为语句中也使用了假列。
由于我无法在数据库中添加两个假列(因为在那里没有用),有没有办法可以使用 Linq 进行插入而忽略这两列?
I have a table in the database with the following columns: ID, Name, Txt
. We are using Linq To Sql to implement our DAL. In there another collegue added two extra columns so in the code the same table results: ID, Name, Txt, NameTemp, TxtTemp
.
These two "fake" tables are used in different parts of the code in LINQ joins and analyzing with SQL Profiler the parsed SQL query takes the "real" columns and everything works properly.
Now I need to make an INSERT using that table, but I get an exception since also the fake columns are used in the statement.
Since I cannot add the two fake columns in the DB(since unuseful there), is there a way in which I could make an insert with Linq omitting these two columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我知道你在说什么。您应该能够向部分 linq 类添加属性,没有问题,唯一的问题是,如果您尝试对这些“假”列使用 linq 查询,则当 linqtosql 尝试引用不存在的列时,您将收到异常数据库中不存在。我以前经历过这个 - 我希望能够选择数据库中不存在的列(但在 linq2sql dbml 类中存在),并让 linq2sql 将这些列转换为数据库中真正存在的列。唯一的问题是,没有真正简单的方法来做到这一点 - 您可以向“假”属性添加属性,以便 linq2sql 认为 NameTmp 和 TxtTmp 实际上是 sql 世界中的 Name 和 Txt,唯一的问题是,当涉及到插入记录时,翻译后的 sql 指定同一列两次(SQL 不喜欢这种情况并引发异常)。
您可以使用 IsDbGenerate = true 标记该列 - 这将允许您插入记录而不会出现双列问题,但是您无法更新记录,除非 linqtosql 抱怨您无法更新计算列。我想你可以使用存储过程来解决这个问题吗?
不久前我向微软记录了一个错误,他们永远不会修复这个错误。此处的信息可能会帮助您获得所需的信息 -
http://social.msdn.microsoft.com/Forums/eu/linqtosql/thread/5691e0ad-ad67-47ea-ae2c-9432e4e4bd46
https://connect.microsoft.com/VisualStudio/feedback/details/526402/linq2sql-doesnt-like-it-when-you-wrap-column-properties-with-properties-in-an-界面
I think i know where you're getting at. You should be able to add properties to a partial linq class no problem, only thing is that if you try and use a linq query against these "fake" columns, you'll get an exception when linqtosql tries to reference a column that doesn't exist in the database. I've been through this before - i wanted to be able to select columns that don't exist in the database (but do in the linq2sql dbml class) and have linq2sql translate the columns into what they really are in the database. Only problem is that there's no real easy way to do this - you can add attributes to the "fake" properties so that linq2sql thinks that NameTmp and TxtTmp are in fact Name and Txt in the sql world, only problem is that when it comes to inserting a record, the translated sql specifies the same column twice (which SQL doesn't like and throws an exception).
You can mark the column with IsDbGenerated = true - that'll let you insert records without getting the double column problem, but you can't update a record without linqtosql complaining that you can't update a computed column. I guess you can use a sproc to get around this perhaps?
I logged a bug with Microsoft a while back, which they'll never fix. The info here might help you get what you need -
http://social.msdn.microsoft.com/Forums/eu/linqtosql/thread/5691e0ad-ad67-47ea-ae2c-9432e4e4bd46
https://connect.microsoft.com/VisualStudio/feedback/details/526402/linq2sql-doesnt-like-it-when-you-wrap-column-properties-with-properties-in-an-interface
LINQ 不是用于插入数据,而是仅用于查询 - 语言集成查询。使用 ADO.NET 插入数据。
(留下第一部分来提醒我的愚蠢)
检查 斯科特·顾。生成的类是部分的(提到 此处),因此您可以将 2 个属性放入可编辑部分,并且由于它们没有定义任何映射属性,因此它们不会被映射或保留。
LINQ is not for inserting data, but for querying only - Language INtegrated Query. Use ADO.NET for inserting the data.
(Leaving the first part to remind my stupidity)
Check ScottGu. The classes generated are partial (mentioned here), so you can put your 2 properties into the editable part and since they won't have any mapping attribute defined, they won't be mapped nor persisted.