Base solution for your next web application
Open Closed

TEntity only supports Int datatype? #527


User avatar
0
lauriep created

Hi,

I have overridden the Id for all entities with code like this:

[Table("Supplier")]
public class Supplier : FullAuditedEntity<Guid>
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public override Guid Id { get; set; }

All works good for DB migrations etc.

However, when trying to create a service for my entities, I get this:

The type 'goMapDo_Offer.gmdSupplier.Supplier' cannot be used as type parameter 'TEntity' in the generic type or method 'IRepository<TEntity>'. There is no implicit reference conversion from 'goMapDo_Offer.gmdSupplier.Supplier' to 'Abp.Domain.Entities.IEntity<int>'

This is because the IRepository interface only supports int:

public interface IRepository<TEntity> : IRepository<TEntity, int>, IRepository, ITransientDependency where TEntity : class, IEntity<int>

I can live with SQL Server type BigInt instead of Int (which just will not store enough values) and have done this instead:

public class Supplier : FullAuditedEntity<Int64>

However, I would prefer to use Guid for architectural reasons (pre-generation).

Any suggestions please?

I notice that the auth classes uses bigint so assume the ABP framework will handle it okay? I mean, not starting weird errors when Ids go beyond 2,147,483,647? Which, in my site, it soon will.

However, if you think I am going to hit subtle issues like this all over the place when using Guid, I will stick with BigInt.

Apologies for the long post.

Cheers.


3 Answer(s)
  • User Avatar
    0
    byteplatz created

    We also use Guid for majority of entities....

    I can see few things to consider:

    Make sure the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] is generating the correct migration like [Id] [uniqueidentifier] NOT NULL DEFAULT newsequentialid(), otherwise, Guids are not created and you need to generate it in code.

    Another thing, when you use Custom primarytype for entities you also have to use the IRepository<TEntity, TPrimaryKey> myRepository for dependenci injection.

    All IRepository<TEntity> are just wrappers for IRepository<TEntity, int> (ints primary keys)

    Just use this DI in your AppService:

    public IRepository<MyEntityType, Guid> myEntityTypeRepository { get; set; }
    

    Bruno

  • User Avatar
    0
    hikalkan created
    Support Team

    Thanks @bytePlatz.

    Yes, you can always use IRepository<Supplier, Guid> instead of IRepository<Supplier>. See document for more: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Repositories#DocIRepositoryClasses">http://www.aspnetboilerplate.com/Pages/ ... oryClasses</a>

  • User Avatar
    0
    lauriep created

    Thanks hikalkan & bytePlatz. Appreciate the quick response.