Search code examples
c#linq-to-sqlpartial

Error when trying to create a partial class


I have created a Area class using Linq-to-SQL.

Now I want to create a partial class of the same name so I can implement validation.

Error 1 Cannot implicitly convert type 'System.Data.Linq.Table<SeguimientoDocente.Area>' to 'System.Linq.IQueryable<SeguimientoDocente.Models.Area>' C:\Users\Sergio\documents\visual studio 2010\Projects\SeguimientoDocente\SeguimientoDocente\Models\AreaRepository.cs 14 20 SeguimientoDocente

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SeguimientoDocente.Models
{
    public class AreaRepository
    {
        private SeguimientoDataContext db = new SeguimientoDataContext();

        public IQueryable<Area> FindAllAreas()
        {
            return db.Areas;
        }

        public Area GetArea(int id)
        {
            return db.Areas.SingleOrDefault(a => a.ID == id);
        }

        public void Add(Area area)
        {
            db.Areas.InsertOnSubmit(area);
        }

        public void Delete(Area area)
        {
            db.Areas.DeleteOnSubmit(area);
        }

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SeguimientoDocente.Models
{
    public partial class Area
    {

    }
}

Here's a screenshot.

alt text


Solution

  • This is almost certainly because your partial class is not in the right namespace. Go into the .designer.cs file of the LINQ model, look for the generated Area class, and make sure the namespace you wrapped your partial class in matches.

    EDIT

    I just fixed the formatting in your question. The error message does in fact indicate that your partial class is in the wrong namespace.

    Error 1 Cannot implicitly convert type 'System.Data.Linq.Table<SeguimientoDocente.Area>' to 'System.Linq.IQueryable<SeguimientoDocente.Models.Area>'

    As you can see from the error above, you need to change the namespace your partial class is in to be SeguimientoDocente, not SeguimientoDocente.Models. As it stands now, they are two completely different incompatible types that happen to have the same simple name.