Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Id Field in Entity #40


User avatar
0
antcandal created

I have the data model with some database tables with the key column not naming Id or ID. For example ClientId, being autoincrement identity. Have I to change all the key fields to Id or ID? with other names persisting throws error


5 Answer(s)
  • User Avatar
    0
    apexdodge created

    I think if you do ID instead of Id, then you're not following conventions and code-first won't know what to map unless you specify with a [ForeignKey("ClientID")] attribute, etc etc.

    But boilerplate takes care of Ids for you if your data model inherits from Entity<long>, I'm pretty sure. Why not do that?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    You can change mapped column name with two options:

    • You can override Id property and use a Column attribute.
    • Or you can override OnModelCreating and set column name. See: <a class="postlink" href="https://msdn.microsoft.com/en-us/data/jj591617.aspx#1.1">https://msdn.microsoft.com/en-us/data/jj591617.aspx#1.1</a>

    These are all EF stuff.

    Have a nice day.

  • User Avatar
    0
    a. h. created

    <cite>hikalkan: </cite> Hi,

    You can change mapped column name with two options:

    • You can override Id property and use a Column attribute.
    • Or you can override OnModelCreating and set column name. See: <a class="postlink" href="https://msdn.microsoft.com/en-us/data/jj591617.aspx#1.1">https://msdn.microsoft.com/en-us/data/jj591617.aspx#1.1</a>

    These are all EF stuff.

    Have a nice day.

    They are and they aren't.

    I have also had a bugger of a time with this.

    E.g Using ABP... I have had to do things to work around the issue of not having an "Id"

    public Client : Entity {
      [Key]
      public virtual int ClientID {get;set;} 
    }
    
    ...
    OnModelCreating(DBModelBuilder modelBuilder) {
      modelBuilder.Entity<Client>().Ignore(e => e.Id);
    }
    
    ...
    IClientRepository : DBRespositoryBase<Client>, IClientRepository {
    
      public virtual Client GetByID (int ClientID)
            {
                Client client = FirstOrDefault(agent => client .ClientID == ClientID);
    
                if (client == null)
                {
                    throw new Exception("Client not found: " + ClientID);
                }
                return Client;
            }
    }
    
    ClientDTO : EntityDTO {
      public ClientDTO (int ClientID) {
          Id = ClientID;
      }
    }
    
    etc...
    

    I can't use the IRepository's Get statement. Can't use AutoMapper... It is very much a ABP issue for me, unless I'm doing it wrong...

    Can you outline a quick to-do of what needs to be done to completely implement a table that doesn't use an Id in ABP? :D

  • User Avatar
    0
    hikalkan created
    Support Team

    Overriding Id works for me. Example:

    public class Person : Entity
        {
            [Column("PersonId")]
            public override int Id { get; set; }
    
            public virtual string Name { get; set; }
    
            [ForeignKey("PersonId")]
            public virtual ICollection<Phone> Phones { get; set; }
        }
    

    Here, I overrided Id and set Column attribute.

  • User Avatar
    0
    a. h. created

    Thank you.