{"data":{"allMarkdownRemark":{"edges":[{"node":{"frontmatter":{"slug":"/blog/could-not-create-an-instance-of-type-cosmosdb","title":"Could not create an instance of type (CosmosDb upsert)","template":"blogTemplate","date":"April 23, 2021"},"id":"ec46bb0f-171d-5bf5-9ed7-57e6e604d2b7","excerpt":"When upserting a new entity to a Cosmosdb Database I was getting  When using the CosmosDb nuget package you can't specify a custom…","html":"<p>When upserting a new entity to a Cosmosdb Database I was getting </p>\n<pre>\nCould not create an instance of type MyNamespace.IUser. \nType is an interface or abstract class and cannot be instantiated. \nPath 'User.Name', line 1, position 213.\n</pre>\n<p>When using the CosmosDb nuget package you can't specify a custom serialiser as you can with, say, Newtonsoft.</p>\n<p>Instead you can create a constructor which takes a concrete implementation of the interface</p>\n<pre><code>[JsonConstructor]\npublic MyEntity(Status status, SomeOtherType value, User user) : base(user)\n{\n        Status = status;\n        Value = value;\n}\n</code></pre>\n<p>where <code>base(user)</code> calls</p>\n<pre><code>public CosmosEntityBase(IUser user)\n{\n        User = user;\n}\npublic IUser User { get; set; }     \n</code></pre>\n<p>This will be less straightforward if you have multiple implementations of the interface. You'll have to determine which one to instantiate.</p>\n<p>However if the <code>JsonConstructor</code> has the interface <code>IUser</code> in its signature then </p>\n<pre>\nCould not create an instance of type MyNamespace.IUser. Type is an interface or abstract class and cannot be instantiated. Path 'User.Name', line 1, position 213.\n</pre>\n<p>will be returned so that is what you need to avoid.</p>"}}]}}}