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

kobosh 176 Reputation points
2020-12-04T01:00:17.24+00:00

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
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,159 questions
0 comments No comments
{count} votes

Accepted answer
  1. kobosh 176 Reputation points
    2020-12-04T16:36:57.497+00:00

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

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,511 Reputation points Microsoft Vendor
    2020-12-04T09:28:37.573+00:00

    Hi @kobosh ,

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. kobosh 176 Reputation points
    2020-12-04T16:36:57.323+00:00

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.