<p>I'm using repository pattern with LINQ, have IRepository.DeleteOnSubmit(T Entity). It works fine, but when my entity class has interface, like this:  </p>

<pre><code>public interface IEntity { int ID {get;set;} }

public partial class MyEntity: IEntity {

    public int ID { 
        get { return this.IDfield; }
        set { this.IDfield=value;  }
    }
}
</code></pre>

<p>and then trying to delete some entity like this:  </p>

<pre><code>IEntity ie=repository.GetByID(1);
repoitory.DeleteOnSubmit(ie);
</code></pre>

<p>throws<br>
The member 'IEntity.ID' has no supported translation to SQL. </p>

<p>fetching data from DB works, but delete and insert doesn't. How to use interface against DataContext?</p>

<hr>

<p>Here it is:<br>
Exception message: 
The member 'MMRI.DAL.ITag.idContent' has no supported translation to SQL.  </p>

<p>Code:  </p>

<pre><code>var d = repContent.GetAll().Where(x =&gt; x.idContent.Equals(idContent));
foreach (var tagConnect in d)    &lt;- error line
{
    repContet.DeleteOnSubmit(tagConnect);
</code></pre>

<p>(it gets all tags from DB, and deletes them)</p>

<p>And stack trace:  </p>

<pre><code>[NotSupportedException: The member 'MMRI.DAL.ITag.idContent' has no supported translation to SQL.]
   System.Data.Linq.SqlClient.Visitor.VisitMember(SqlMember m) +621763
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +541
   System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +8
   System.Data.Linq.SqlClient.SqlVisitor.VisitBinaryOperator(SqlBinary bo) +18
   System.Data.Linq.SqlClient.Visitor.VisitBinaryOperator(SqlBinary bo) +18
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +196
   System.Data.Linq.SqlClient.SqlVisitor.VisitExpression(SqlExpression exp) +8
   System.Data.Linq.SqlClient.SqlVisitor.VisitSelectCore(SqlSelect select) +46
   System.Data.Linq.SqlClient.Visitor.VisitSelect(SqlSelect select) +20
   System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) +1024
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery( ...
</code></pre>

<p>When I try do decorate partial class:</p>

<pre><code>[Column(Storage = "_idEvent", DbType = "Int NOT NULL", IsPrimaryKey = true)]
public int idContent
{ get { return this.idEvent; } set { this.idEvent=value; } }
</code></pre>

<p>it throws error "Invalid column name 'idContent'."</p>

[{'answer_id': 14381, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 0, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>using System.Data.Linq.Mapping;\n\npublic partial class MyEntity: IEntity \n {    [Column(Storage="IDfield", DbType="int not null", IsPrimaryKey=true)]\n      public int ID \n       {         \n          get { return this.IDfield; }        \n          set { this.IDfield=value;  }    \n       } \n }\n</code></pre>\n'}, {'answer_id': 2069586, 'author': 'Frank Tzanabetis', 'author_id': 251210, 'author_profile': 'https://Stackoverflow.com/users/251210', 'pm_score': 2, 'selected': False, 'text': '<p>This works for me - </p>\n\n<pre><code>public partial class MyEntity: IEntity \n {    [Column(Name = "IDfield", Storage = "_IDfield", IsDbGenerated = true)]\n      public int ID \n       {         \n          get { return this.IDfield; }        \n          set { this.IDfield=value;  }    \n       } \n }\n</code></pre>\n'}, {'answer_id': 2070758, 'author': 'jeroenh', 'author_id': 20047, 'author_profile': 'https://Stackoverflow.com/users/20047', 'pm_score': 0, 'selected': False, 'text': '<p>For translating your LINQ query to actual SQL, Linq2SQL inspects the expression you give it. The problem is that you have not supplied enough information for L2S to be able to translate the "ID" property to the actual DB column name. You can achieve what you want by making sure that L2S can map "ID" to "IDField". </p>\n\n<p>This should be possible using the approach provided in answers. </p>\n\n<p>If you use the designer, you can also simply rename the class property "IDField" to "ID", with the added benefit that you won\'t have to explicitly implement the "ID" property in your partial class anymore, i.e. the partial class definition for MyEntity simply becomes:</p>\n\n<pre><code>public partial class MyEntity: IEntity \n{    \n}\n</code></pre>\n'}, {'answer_id': 27485863, 'author': 'jahu', 'author_id': 2123652, 'author_profile': 'https://Stackoverflow.com/users/2123652', 'pm_score': 3, 'selected': False, 'text': "<p>It appears Microsoft dropped support for <code>==</code> operator in interfaces when using linq-to-sql in MVC4 (or maybe it was never supported). You can however use <code>i.ID.Equals(someId)</code> in place of the <code>==</code> operator.</p>\n\n<p>Casting <code>IQueryable</code> to <code>IEnumerable</code> works but <strong>should not be used!</strong> The reason is: <code>IQueryable</code> has funky implementation of <code>IEnumerable</code>. Whatever linq method you'll use on a <code>IQueryable</code> through the <code>IEnumerable</code> interface will cause the query to be executed first, have all the results fetched to the memory from the DB and eventually running the method localy on the data (normally those methods would be translated to SQL and executed in the DB). Imagine trying to get a single row from a table containing billion rows, fetching all of them only to pick one (and it gets much worse with careless casting of <code>IQueryable</code> to <code>IEnumerable</code> and lazy loading related data).</p>\n\n<p>Apparently Linq has no problem using <code>==</code> operator with interfaces on local data (so only <code>IQueryable</code> is affected) and also with Entity Frameworks (or so I heard).</p>\n"}]
['https://Stackoverflow.com/questions/14375', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1407/']