question

kobosh-5924 avatar image
0 Votes"
kobosh-5924 asked kobosh-5924 edited

Linq to sql class error: must add reference to assembly Syste.data.linq

I created .Net Framework web form app in VS 2019 community. I added linq tosql class (dmbl) using table from AdventureWorks CountryRegions table. The class was created successfully . However I get error in my Business class (Bal): Error says:
CS0012 The type 'Table<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I can see System.Data.Linq added to the references.
And System.Linq in Nuget packages.


 using Microsoft.Owin;
 using Owin;
 using System;
 using System.Threading.Tasks;
 //using System.Data.Common
    
 using System.Linq;
 using System.Collections.Generic;
    
 //[assembly: OwinStartup(typeof(OBjectDSDemo.App_Code.Biz.Bal))]
    
 namespace OBjectDSDemo.App_Code.Biz
 {
     public class Bal
     {
         public List<CountryRegion> GetCustomers()
    
         {
             var contxt = new DataClasses1DataContext();
    
             //customers=from c in 
    
             return (from c in contxt.CountryRegions select c.Country).toList();
    
         }
     }
 }
    
 #pragma warning disable 1591
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     This code was generated by a tool.
 //     Runtime Version:4.0.30319.42000
 //
 //     Changes to this file may cause incorrect behavior and will be lost if
 //     the code is regenerated.
 // </auto-generated>
 //------------------------------------------------------------------------------
    
 namespace OBjectDSDemo
 {
     using System.Data.Linq;
     using System.Data.Linq.Mapping;
     using System.Data;
     using System.Collections.Generic;
     using System.Reflection;
     using System.Linq;
     using System.Linq.Expressions;
     using System.ComponentModel;
     using System;
        
        
     [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="AdventureWorks")]
     public partial class DataClasses1DataContext : System.Data.Linq.DataContext
     {
            
         private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
            
     #region Extensibility Method Definitions
     partial void OnCreated();
     partial void InsertCountryRegion(CountryRegion instance);
     partial void UpdateCountryRegion(CountryRegion instance);
     partial void DeleteCountryRegion(CountryRegion instance);
     #endregion
            
         public DataClasses1DataContext() : 
                 base(global::System.Configuration.ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ConnectionString, mappingSource)
         {
             OnCreated();
         }
            
         public DataClasses1DataContext(string connection) : 
                 base(connection, mappingSource)
         {
             OnCreated();
         }
            
         public DataClasses1DataContext(System.Data.IDbConnection connection) : 
                 base(connection, mappingSource)
         {
             OnCreated();
         }
            
         public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
                 base(connection, mappingSource)
         {
             OnCreated();
         }
            
         public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : 
                 base(connection, mappingSource)
         {
             OnCreated();
         }
            
         public System.Data.Linq.Table<CountryRegion> CountryRegions
         {
             get
             {
                 return this.GetTable<CountryRegion>();
             }
         }
     }
        
     [global::System.Data.Linq.Mapping.TableAttribute(Name="Person.CountryRegion")]
     public partial class CountryRegion : INotifyPropertyChanging, INotifyPropertyChanged
     {
            
         private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
            
         private string _CountryRegionCode;
            
         private string _Name;
            
         private System.DateTime _ModifiedDate;
            
     #region Extensibility Method Definitions
     partial void OnLoaded();
     partial void OnValidate(System.Data.Linq.ChangeAction action);
     partial void OnCreated();
     partial void OnCountryRegionCodeChanging(string value);
     partial void OnCountryRegionCodeChanged();
     partial void OnNameChanging(string value);
     partial void OnNameChanged();
     partial void OnModifiedDateChanging(System.DateTime value);
     partial void OnModifiedDateChanged();
     #endregion
            
         public CountryRegion()
         {
             OnCreated();
         }
            
         [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CountryRegionCode", DbType="NVarChar(3) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]
         public string CountryRegionCode
         {
             get
             {
                 return this._CountryRegionCode;
             }
             set
             {
                 if ((this._CountryRegionCode != value))
                 {
                     this.OnCountryRegionCodeChanging(value);
                     this.SendPropertyChanging();
                     this._CountryRegionCode = value;
                     this.SendPropertyChanged("CountryRegionCode");
                     this.OnCountryRegionCodeChanged();
                 }
             }
         }
            
         [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Name", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
         public string Name
         {
             get
             {
                 return this._Name;
             }
             set
             {
                 if ((this._Name != value))
                 {
                     this.OnNameChanging(value);
                     this.SendPropertyChanging();
                     this._Name = value;
                     this.SendPropertyChanged("Name");
                     this.OnNameChanged();
                 }
             }
         }
            
         [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ModifiedDate", DbType="DateTime NOT NULL")]
         public System.DateTime ModifiedDate
         {
             get
             {
                 return this._ModifiedDate;
             }
             set
             {
                 if ((this._ModifiedDate != value))
                 {
                     this.OnModifiedDateChanging(value);
                     this.SendPropertyChanging();
                     this._ModifiedDate = value;
                     this.SendPropertyChanged("ModifiedDate");
                     this.OnModifiedDateChanged();
                 }
             }
         }
            
         public event PropertyChangingEventHandler PropertyChanging;
            
         public event PropertyChangedEventHandler PropertyChanged;
            
         protected virtual void SendPropertyChanging()
         {
             if ((this.PropertyChanging != null))
             {
                 this.PropertyChanging(this, emptyChangingEventArgs);
             }
         }
            
         protected virtual void SendPropertyChanged(String propertyName)
         {
             if ((this.PropertyChanged != null))
             {
                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
     }
 }
 #pragma warning restore 1591

dotnet-runtimedotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kobosh-5924 avatar image
0 Votes"
kobosh-5924 answered kobosh-5924 edited

thanks Xudong it works. I only needed to restart visual studio

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XuDongPeng-MSFT avatar image
1 Vote"
XuDongPeng-MSFT answered

Hi @kobosh-5924 ,

According to your description, I did some related searches on this issue.

And here is a similar case, you could try:

  • Navigate to the web project's References node

  • Find the reference to System.Data.Linq

  • Open the VS Properties Window

  • In the properties window, change Copy Local: False to True

Or you could try copy the System.Data.Linq dll reference to bin directory.

Best regards,
Xudong Peng



If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kobosh-5924 avatar image
0 Votes"
kobosh-5924 answered kobosh-5924 edited

thanks Xudong it works. I only needed to restart visual studio

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.