Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL not supported in .net 5.0?

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?

like image 724
Vamos Avatar asked Apr 23 '21 08:04

Vamos


People also ask

Is LINQ to SQL still supported?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

Can we use LINQ in .NET core?

NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced.

Can we use LINQ in asp net?

You can use LINQ through the LinqDataSource control, through the ObjectDataSource control, or by creating LINQ queries. When you use LINQ in a Web application, you might have to change the policy files for code-access security.

Is LINQ converted to SQL?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.


1 Answers

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.

like image 62
Vamos Avatar answered Oct 19 '22 00:10

Vamos