Base solution for your next web application
Open Closed

How to create Entity with existing database table? #3326


User avatar
0
fguo created

I need to create an entity with an existing table, so I have to use the existing column name, especially the prime key column. That means I cannot inherit the FullAuditedEntity, otherwise, it will add a key column “Id” and other predefined columns. The problem happens while I create the AppService on the IRepository<TEntity>. It seems that the TEntity must inherit the FullAuditedEntity. Otherwise, there is a compile error:

There is no implicit reference conversion from ‘Custom-Type’ to ‘Abp.Domain.Entities.IEntity<int>’.

Here is my sample code for Entity. The table “Customers” is an existing table with a prime key “SCM_ID”. I have to remove the “FullAuditedEntity”:

[Table("Customers")]
public class Customer //: FullAuditedEntity
{
    public const int MaxNameLength = 32;

    [Required]
    [Key]
    public int SCM_ID { get; set; }

    [MaxLength(MaxNameLength)]
    public virtual string SCM_Name { get; set; }
}

Here is my sample code for AppService:

public class CustomerAppService : MyAppServiceBase, ICustomerAppService
{
    private readonly IRepository&lt;Customer&gt; _customerRepository; //ERRORS HERE!!

    public ListResultDto&lt;CustomerListDto&gt; GetCustomers(GetCustomersInput input)
    {
        var customers = _customerRepository
            .GetAll()
            .ToList();

        return new ListResultDto&lt;CustomerListDto&gt;(ObjectMapper.Map&lt;List&lt;CustomerListDto&gt;>(customers));
    }
}

The compile error is on the line of “IRepository<Customer>”:

The type ‘…Customer’ cannot be used as type parameter ‘TEntity’ in the generic type or method ‘IRepository<TEntity>’. There is no implicit reference conversion from ‘…Customer’ to ‘Abp.Domain.Entities.IEntity<int>’

I tried to force the Entity and the ListDto class to inherit the “FullAuditedEntity”. It can pass the compile, but it has an error on run-time: the “_customerRepository” seems never assigned to and always null.

Please advise a correct way for entities with existing database table.

Thanks,


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you want to use generic repositories, you don't have to inherit from FullAuditedEntity but you have to inherit from Entity class. It will add Id property to your entity. You can map Id field of your entity to SCM_ID of your database column using Entity Framework mappings. For exmaple in onModelCreating of your DbContext:

    modelBuilder.Entity<YourEntity>().Property(x => x.Id).HasColumnName("SCM_ID ");
    

    Thanks.

  • User Avatar
    0
    fguo created

    I tried same thing as you instructed:

    Let MyEntity inherit from Abp.Domain.Entities.Entity class, Map Id field of my entity to SCM_ID by adding the code into onModelCreating of the DbContext.

    But it still has a run time error around the line: private readonly IRepository<Customer> _customerRepository;

    The “_customerRepository” seems never assigned to and always null.

    Do you mean I have to remove "IRepository<Customer>", and build my own Repository class?

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Custom repository will work but my previous suggestion was not that. Do you still get the same error message after making those changes ?

    Thanks.

  • User Avatar
    0
    fguo created

    After making those changes, there is no error on compile time, but there is a warning on "_customerRepository", said it "is never assigned to, and will always have its default value null."

  • User Avatar
    0
    fguo created

    My fault! I forgot to add constructor in AppService class. I fixed it and it works as expected. :D

    I only test GET on this point, and still have a little concern about POST and PUT, because the database table has no such automatically generated columns (e.g. CreationTime, CreatorUserId, DeletionTime, DeleterUserId, IsDeleted, and so on). While POST or PUT, I am afraid it will try to add values into these not-existing columns and cause errors.

    Is it possible to happen? I will continue to test.

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Good :). If you use Entity instead of FullAuditedEntity there will be no such fields in your entity and creating/saving an entity will not be a problem.

    Thanks.