-1

I have a bass class like this:

public abstract class Document
{
[Dapper.Contrib.Extensions.Key]
public Int32 DoucmentID {get;set;}
public String Name {get;set;}
}

Then I have

public class Proposal : Document
{
   public String ProposalStuff {get;set;}
}

Now I want to write some generic method to handle updates using Dapper.Contrib

//some class somewhere..

public bool Update<T> (T as item) where T : class
{
 using (var sqlConnection = new SqlConnection(_connectionString))
            {
                sqlConnection.Open();
                return sqlConnection.Update<T>(item);
            }
}

Now I want to update the object:

   public bool UpdateProposal(Repository.Proposal prop)
   {
            return orm.UpdateItem<Repository.Proposal>(prop);
   }

Dapper.Contrib gives me this message:

{"Entity must have at least one [Key] or [ExplicitKey] property"}

I am not able to find an example using an abstract base class. I thought maybe the type for the UpdateProposal should be Document but I get the same message.

Thanks for the help. I am sure it is something simple.

EDIT: I have found half the answer. I started testing just using a single class (no abstract) and got the same error. After further research:

Dapper.SimpleCRUD Insert / Update / Get fails with message "Entity must have at least one [Key] property"

I found that using Key [Dapper.Contrib.Extensions.Key] can solve the issue. It seems though that Key from ystem.ComponentModel.DataAnnotations should also work?

If not that makes me a little sad as any service that wants to use the ORM would have to know about Dapper and I was hoping to avoid a mapper.

Going to try now and break up the class again though and see if at least it solves the immediate issue.

M.

melegant
  • 1,649
  • 2
  • 12
  • 15

1 Answers1

0

So yes after all it works fine with the abstract class as long as I use the Dapper Key annotation vs. ComponentModel.

It might be that the Dapper Key is only what is supported. but that would be another question.

M.

melegant
  • 1,649
  • 2
  • 12
  • 15