Trying to implement a generic repository based on an article by Chris Pratt that uses object
as the Id
(PK).
All is good, until I tried to .Map()
one of these objects with ExpressMapper.
More code below. But essentially, when I try and do this:
var dataModel = postedModel.Map(new ExampleDataModel());
It gives me:
No parameterless constructor defined for this object.
When I change the Id
property of ExampleDataModel
to a string
or int
it works fine.
I've googled around. I don't see anything obvious, but I am a bit out of my depth. I'd like to know:
What is causing this?
Can I work round it somehow?
I'd like to continue using the object PK and ExpressMapper if possible because they're both suiting my project just fine.
public interface IDataModel
{
object Id { get; }
}
public interface IDataModel<PKT> : IDataModel
{
new PKT Id { get; set; }
}
public abstract class DataModel<PKT> : IDataModel<PKT>
{
public PKT Id { get; set; }
object IDataModel.Id
{
get { return this.Id; }
}
}
public class ExampleDataModel :
DataModel<string>,
IDataModel<string>
{
public virtual string SomeProperty{ get; set; }
// etc.
}
when you are doing new ExampleDataModel()
, You are trying to initalize an instance of ExampleDataModel
with parameterless constructor, but looking at your class, you don't have one, that's why you are getting
No parameterless constructor defined for this object.
You should add one parameterless constructor, which looks like this
ExampleDataModel()
{
//init object
}
though I am not sure how you want to init your object so I am not sure how your constructor should be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With