Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:55:52+00:00 2026-06-15T12:55:52+00:00

I am using EF and AutoMapper with Azure table storage emulation. I have two

  • 0

I am using EF and AutoMapper with Azure table storage emulation.

I have two types, Fragment (serializable DTO) and FragmentEntity (EF persisted DO). I use AutoMapper to map from the DTO to the DO for persistence on a PUT call and vice versa on a GET call.

The problem is my inexperience with both EF and AutoMapper. I have configured the FragmentEntity class (according to the documentation I can find) to be generating the Id for the Fragment at the database (see below).

Entity ID definition:

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 public int? Id { get; set; }

At runtime the http client makes a REST PUT call with the new content (Fragment). This is received via WCF and serialized into a Fragment as follows.

 [OperationContract]
 [WebInvoke(UriTemplate = "/Fragments", Method = "PUT")]
 void PutFagment(Fragment fragment);

During debugging if I step over the code the received ID value is null (as I have defined the ID property as optional).

 public void PutFagment(Fragment fragment)
 {
      var fragmentEntity = Mapper.Map<Fragment, FragmentEntity>(fragment);
      _fragmentContext.Add(fragmentEntity);
 }

 [DataMember]
 public virtual int? Id { get; set; }

I am not sure if defining it as optional was a wise move; I did this because if it is not optional the value defaults to zero which automapper then maps to the target FragmentEntity and subsequently all keys end up being zero (clearly wrong) in the database.

As you may expect I have also made the FragmentEntity Id optional to avoid setting a value in the application code, my hope was if it was null the database generation may occur however this doesn’t appear to be happening.

<Fragment><Id i:nil="true"/><Name>Drei</Name><Type>3</Type></Fragment>

My problems:

  1. I think the decision to make Id optional in both classes is incorrect, I assume there is some AutoMapper configuration which will appropriately handle this case and that both can remain non-optional.
  2. I am not sure why the DatabaseGenerated attribute is not taking effect.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:55:53+00:00Added an answer on June 15, 2026 at 12:55 pm

    So, it turns out that the developer is responsible with azure table storage for generating their unique rowkey. This is done for the FragmentEntity which extends TableServiceEntity. Aka the provider will not support database key generation, the developer must own this.

    This means:

    1. You must own the ID generation code.
    2. You do not need to define Id attributes in your TableServiceEntitys, instead, just provide a sensible mapping in the AutoMapper configuration in both directions.

    The ID should not be specified by the client (it is a server responsibility). So the mapping from DTO to persisted DO overwrites whatever value is set by default with a newly generated Guid (see code below).

    Mapper.Configuration.CreateMap<Fragment, FragmentEntity>()
          .ForMember(dest => dest.RowKey,
                     expression => expression.ResolveUsing(source => Guid.NewGuid().ToString()))
    // NB: you are also required to provide a partition key for TableServiceEntities
          .ForMember(dest => dest.PartitionKey,
                     opt => opt.ResolveUsing(source => PartitionKeyConstants.LocalPartitionKey));
    

    Then the mapping in the other direction from the FragmentEntity to the Fragment must be configured to set the Id attribute of the DTO to the RowKey value of the FragmentEntity.

    Mapper.Configuration.CreateMap<FragmentEntity, Fragment>()
          .ForMember(dest => dest.Id, 
                     expression => expression.ResolveUsing(source => source.RowKey));
    

    My mapping code is registered in my Global.asax.cs to handle the mapping registrations. I will move this into an IoC container at some point.

    As a result of this change I no longer define an ID in my FragmentEntity and the defined Id attribute in the Fragment class is no longer optional.

    [DataMember]
    public virtual Guid Id { get; set; }
    

    NB: I used Guid because it is easier to generate unique Guid values than to track int keys and increment. Also this means I’m not introducing a db-lock bottleneck while checking which int key value is next.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I using Automapper. I have two classes: TypeA with single property; TypeB with two
I have been using Automapper in our projects and just recently ran into an
I have recently started using automapper and it has work fine for me so
I have this interface for using AutoMapper: public interface IMapper { object Map(object source,
Does anyone have a technique to automap (using Automapper) references to child entities. So
I am using Automapper in my project to map business entities to DTO. public
I am using AutoMapper.org to map my DTO objects to Model objects in MVC4.
I have been using automapper pretty successfully lately but i have come across a
I am using AUtomapper which I am very impressed with however, I have a
Hi I am using AutoMapper to move from a Model to a Dto and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.