11

My project used .NetFramework 4.6.2 with "LINQ to SQL" query from MSSQL. A class define all tables which are needed query in database and inheritance to DataContext(System.Data.Linq).

Recently, I am going to upgrade the program to .net5.0. However, I discover "LINQ to SQL" seems like doesn't support to .net5.0.

Could anyone advise how to write "LINQ to SQL" in .net5.0 or using other ways please?

Thanks.

--

Apr 23 PM6:38 Update: I just found SqlSugarCore on Nuget, and test it is okay to use. But still wondering is there any similar tools could use?

Vamos
  • 209
  • 2
  • 10
  • Does this answer your question? [Has Microsoft confirmed their stance on LINQ to SQL end-of-life?](https://stackoverflow.com/questions/3407565/has-microsoft-confirmed-their-stance-on-linq-to-sql-end-of-life) – Franz Gleichmann Apr 23 '21 at 08:04
  • Did you use any ORM on your project with .NetFramework 4.6.2? – sajadre Apr 23 '21 at 08:05
  • @sajadre do you mean like "Dapper"? I didn't use that. I build a class which list tables needed query in database and this class is inheritance to DataContext(System.Data.Linq). – Vamos Apr 23 '21 at 08:12
  • @FranzGleichmann thanks for your reply. But I am not asking about it. >_ – Vamos Apr 23 '21 at 08:14
  • @MickyD Thanks. I was trouble to define it. But I also want to ask about how to write LINQ to database "with the similar way" in .net5.0 – Vamos Apr 23 '21 at 08:16
  • 1
    @MickyD and even back then L2S was basically EOL. and even back then, the solution was to simply switch to entity framework. which qualifies as "using other ways" – Franz Gleichmann Apr 23 '21 at 08:19
  • 3
    The closest thing to L2S in .NET 5 is: EF Core; there are some differences, but it *mostly* works similarly; we migrated without much drama. – Marc Gravell Apr 23 '21 at 08:50
  • 1
    @MickyD "OP has already indicated he doesn't want an ORM" - but OP is using L2S, which is already at least one-foot-through-the-door into ORM land; is it as opinionated as "full" ORMs like EF, LLBLGen, etc: no, but it also isn't "not an ORM" – Marc Gravell Apr 23 '21 at 08:53
  • 1
    Did you use EFCore or something like that? Do you have any DBContext settings in your project? – sajadre Apr 23 '21 at 08:53
  • 1
    The official replacement is Entity Framework. There is also the unofficial fork of L2S at https://github.com/FransBouma/LinqToSQL2 or https://github.com/albahari/LinqToSQL2 (which I assume is what Linqpad uses) – sgmoore Apr 23 '21 at 14:12
  • 1
    Just install the appropriate workload with Visual Studio, and EF core 5 SDK, and L2S will be in your tools. – Gert Arnold Apr 23 '21 at 14:22
  • @GertArnold Thanks. It's work! – Vamos May 07 '21 at 03:36
  • @sajadre Thanks. After add DbContext settings(class of table and database), my project is worked. :) – Vamos May 07 '21 at 03:38
  • @MarcGravell yeah, as you said, it works to me too! – Vamos May 07 '21 at 03:39
  • @sgmoore I was not familiar to Entity Framework, thus I cannot understand what some comments talked about. After some hard works(adjust codes, read EFCore related articles..), my project worked fine with EFCore now. Thank you. :) – Vamos May 07 '21 at 03:46
  • @Vamos - Please do not put SOLVED in your title and don't put the answer in your question. You should post an actual answer and then mark that answer as accepted. Can you please make that change? – Enigmativity May 07 '21 at 03:51
  • @Enigmativity Oh, my fault! Sorry for that I use Stackoverflow not a long time. I will make this change, thanks for your kindness to let me know. – Vamos May 07 '21 at 07:28
  • @GertArnold After installing the respective workloads, Linq2Sql does indeed appear as an option in the "Add New Item" dialog, and it can indeed be added, but it then doesn't work because the generated code is not compatible with .NET 5 (relies on [System.Data.Linq](https://stackoverflow.com/a/45935875/11683)). – GSerg Oct 17 '21 at 14:35
  • @MarcGravell *we migrated without much drama* - did the migration involve any database-first scenarios? I find that EF Core's native support for database-first is abysmal to non-existent, apart from resorting to third-party extensions such as listed [here](https://learn.microsoft.com/en-us/ef/core/extensions/). – GSerg Oct 17 '21 at 14:54

1 Answers1

9

I have tried to use EFCore and it's work.

Just install Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer in NuGet.

Replace those showed errors from L2S to EFCore. Example: DataContext(L2S) => DbContext(EFCOre), public Table TableA;(L2S) => public DbSet TableA { get; set; }(EFCore)...etc

Be careful of that if your table have more than one primary key, then you have to write override OnModelCreating as belowed, TableAClass has three keys, and TableBClass has one key.

。Setup key of per table

protected override void OnModelCreating(ModelBuilder _modelBuilder)
        {
            _modelBuilder.Entity<TableAClass>().HasKey(_obj => new { _obj.Key1, _obj.Key2, _obj.Key3 });
            _modelBuilder.Entity<TableBClass>().HasKey(_obj => new { _obj.Key1 });
        }

I have got stuck few days, and couldn't find an example on internet. Thus, I decided to put my code here.

。Setup Database Class

public class DBClassName : DbContext
    {
        public DbSet<TableAClass> TableAs { get; set; }
        public DbSet<TableBClass> TableBs { get; set; }

        protected override void OnModelCreating(ModelBuilder _modelBuilder)
        {
            _modelBuilder.Entity<TableAClass>().HasKey(_obj => new { _obj.Key1, _obj.Key2, _obj.Key3 });
            _modelBuilder.Entity<TableBClass>().HasKey(_obj => new { _obj.Key1 });
        }

        public DBClassName(string _connStr) : base(GetOptions(_connStr))
        {
        }

        private static DbContextOptions GetOptions(string connectionString)
        {
            return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder<DBClassName>(), connectionString).Options;
        }


    }

。Setup Table Class

[Table("TableName")]
    public class TableClassName
    {
        public string ColumnA { get; set;}

        public string ColumnB { get; set;}

    }

This answer should credits to all who helping me in comments.

Vamos
  • 209
  • 2
  • 10