'Converts this Entity to its protocol buffer representation. Not
intended to be used by application developers.
Returns:
entity_pb.Entity'

def _ToPb(self, mark_key_as_saved=True):

    pb = entity_pb.EntityProto()
    pb.mutable_key().CopyFrom(self.key()._ToPb())
    last_path = pb.key().path().element_list()[(-1)]
    if (mark_key_as_saved and last_path.has_name() and last_path.has_id()):
        last_path.clear_id()
    group = pb.mutable_entity_group()
    if self.__key.has_id_or_name():
        root = pb.key().path().element(0)
        group.add_element().CopyFrom(root)
    properties = self.items()
    properties.sort()
    for (name, values) in properties:
        properties = datastore_types.ToPropertyPb(name, values)
        if (not isinstance(properties, list)):
            properties = [properties]
        for prop in properties:
            if ((prop.has_meaning() and (prop.meaning() in datastore_types._RAW_PROPERTY_MEANINGS)) or (name in self.unindexed_properties())):
                pb.raw_property_list().append(prop)
            else:
                pb.property_list().append(prop)
    if (pb.property_size() > _MAX_INDEXED_PROPERTIES):
        raise datastore_errors.BadRequestError(('Too    many    indexed    properties    for    entity    %r.' % self.key()))
    return pb
