0

i am trying to use Linq to Sql technology, but encounter some problems.

Here is my simple code.

[Table]
    public class countries
    {
        [Column(IsPrimaryKey = true)] public int Id;
        [Column] public string Country;
        [Column] public int Continent;
    }
class Program
{

    static void Main(string[] args)
    {

        string connection = "SERVER=localhost; DATABASE=advworks; UID=root; PWD=root;";

        DataContext dataContext = new DataContext(connect.connection);


        Table<countries> table = dataContext.GetTable<countries>();

        var query = from n in table
                    select n.Country;



        foreach (var item in query)
            Console.WriteLine(item);

        Console.ReadKey();

    }
}

I have MySql server with appropriate shema and table. The problem is that query generates this - SELECT [t0].[Country] FROM [countries] AS [t0] but when i start the program sqlexception is thrown because of syntax error.

i have try to input - SELECT [t0].[Country] FROM [countries] AS [t0] in my sql directly, and it worked only after i removed square brackets.

Why linq generates this square brackets? Or there is problem with my sql server?

pix
  • 1,264
  • 19
  • 32
Eugen
  • 19
  • 5
  • 3
    Is it MySql or SqlServer? Those are two different products. The syntax of the generated query will work with SQL Server, but not with MySql. – Zohar Peled Nov 12 '18 at 15:58
  • 1
    Use Entity Framework, Linq-To-Sql works with SQL-Server whereas EF works with [many databases including MySql](https://learn.microsoft.com/en-us/ef/core/providers/). – Tim Schmelter Nov 12 '18 at 15:59
  • I have mysql server. Is there way to generate appropriate sql syntax with Linq to Sql? – Eugen Nov 12 '18 at 16:02
  • Possible duplicate of [how to use LINQ to SQL with mySQL](https://stackoverflow.com/questions/12388347/how-to-use-linq-to-sql-with-mysql) – Eugen Nov 12 '18 at 18:33

0 Answers0