简体   繁体   English

EF6在查询中插入“\\ n”?

[英]EF6 inserting “\n” in query?

Recently I discovered the beauty of Entity Framework 6 and decided to implement it in my current project. 最近我发现了Entity Framework 6的美妙之处,并决定在我当前的项目中实现它。 However, I stumbled upon a weird problem today. 但是,我今天偶然发现了一个奇怪的问题。 I have been searching the internet for hours, but I did not come to any solution yet. 我一直在网上搜索几个小时,但我还没有找到任何解决方案。

First, some info which might be of use; 首先,一些可能有用的信息;

  • We are using MariaDB for our database. 我们正在将MariaDB用于我们的数据库。
  • We are using the MySql provider for EF6. 我们正在使用EF6的MySql提供程序。
  • The connection made is local and is working. 建立的连接是本地的,并且正在工作。 The database correctly responds to correct queries in my application. 数据库正确响应我的应用程序中的正确查询。

The problem: For some reason, EF is generating a query containing "\\n". 问题:由于某种原因,EF正在生成包含“\\ n”的查询。 Since my error goes as follows; 由于我的错误如下;

"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(SELECT\\n table . column1 , \\n table . column2 , \\n table . column3 at line 1" “你在你的SQL语法错误;检查对应于您的MariaDB的服务器版本使用附近的“正确的语法手册(选择\\ n tablecolumn1 ,\\ n tablecolumn2 ,\\ n tablecolumn3第1行“

I literally copied the query in my database interface (which is HeidiSql, for those who are interested) and after I remove the "\\n", the query seems to work fine. 我确实在我的数据库界面(对于那些感兴趣的人是HeidiSql)中复制了查询,在我删除了“\\ n”之后,查询似乎工作正常。

The error is thrown at: 错误发生在:

using(var db = new DataContext())
{
   table tab = new table
   {
      column1 = "Some value",
      column2 = "Some other value",
      column3 = "Yet, another value"
   };
   db.table.Add(tab);
   db.SaveChanges();      //This is where the error occurs
}

I have debugged the input and it is correct. 我调试了输入,这是正确的。 Also, the table is correctly added to the table entity. 此外,该表已正确添加到表实体。 The code also compiles like it should. 代码也按照它应该编译。

Strange thing is, this is the only place where the error occurs (yet - I have to test it more to be sure of that). 奇怪的是,这是发生错误的唯一地方(但是 - 我必须更多地测试它以确保这一点)。 I call db.SaveChanges() in other places and somehow it does not throw any error there. 我在其他地方调用db.SaveChanges() ,并以某种方式它不会抛出任何错误。 In other words; 换一种说法; I am actually able to add and save data to the database for other, more complex tables. 我实际上能够为其他更复杂的表添加数据并将数据保存到数据库中。 This one, somehow, does not seem to work. 不知何故,这个似乎不起作用。

The table does not contain any primary or foreign keys (it really doesn't need any either, I've thought about it.). 该表不包含任何主键或外键(它实际上也不需要任何键,我已经考虑过了。)。

Am I missing something here? 我在这里错过了什么吗?

在EF中,如果没有定义PK,则无法使用映射实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM