How to map column and entity propery of different datatypes in entity framework code first

后端 未结 5 1235
北恋
北恋 2020-12-10 14:33

I am using Entity Framework 5 - Code first.

I have a database that I am connecting to that already exists for some time now (I did not create it). There is a table

相关标签:
5条回答
  • 2020-12-10 14:52

    The simplest solution is to use another field that will be mapped to the database field, and the ID property will read/write to this field. Something like this:

    public class Customer : IEntity
    {
       public decimal CustomerID {get; set;}
    
       [NotMapped]
       public int Id 
       { 
          get { return (int)CustomerID; }
          set { CustomerID = (int)value; } 
       }
    
       public string FirstName { get; set; }
    
       public string LastName { get; set; }
    }
    

    Map this new field to the database field, using

    this.Property(x => x.CustomerID).HasColumnName("Customer_id");
    

    and the EF will use the customer id field, and your code could happily use the integer id field.

    0 讨论(0)
  • 2020-12-10 14:55

    Specify numeric type for column

    Property(x => x.Id).HasColumnName("Customer_id").HasColumnType("numeric");
    

    When generating database, it will create numeric column with precision 18,0. But when you are mapping to existing database, it will work fine with 5,0 numeric column.

    0 讨论(0)
  • 2020-12-10 14:56

    In .Net Core you can also use System.ComponentModel.DataAnnotations.Schema.ColumnAttribute to annotate the property with the underlying column type, e.g.,

        [Column(TypeName = "numeric(5, 0), not null")]
        public int Id { get; set; }
    

    Or if the column is nullable:

        [Column(TypeName = "numeric(5, 0), null")]
        public int? Id { get; set; }
    

    I prefer this to the accepted answer by @Sergey Berezovskiy, as you make the annotation actually on the model, not in elsewhere (in the DbContext).

    The ColumnAttribute can also be used to map a property to a column with a different name, e.g.,

        [Column("Identifier", TypeName = "numeric(5, 0), not null")]
        public int Id { get; set; }
    

    The ColumnAttribute may also work in the .Net Framework - I have not tested it.

    0 讨论(0)
  • 2020-12-10 14:59

    Another option to work out what the code first should be is to the MS EF Powertools http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

    You can add an empty project, right click, EF Reverse engineer code first from DB.

    You often (not always) get a good start to the solution. Did you try using decimal?

    public class Customer : IEntity
    {
     public decimal Id { get; set; }
    
     public string FirstName { get; set; }
    
     public string LastName { get; set; }
    }
    

    if you are concerned precision, you should add validation to the POCO.

    There are also some interesting Anotation hacks you may like. http://geekswithblogs.net/danemorgridge/archive/2010/12/20/ef4-code-first-control-unicode-and-decimal-precision-scale-with.aspx good luck

    0 讨论(0)
  • 2020-12-10 15:07

    You can convert it to an int with:

     int myInt = Convert.Int32(numberFromDatabase);
    
    0 讨论(0)
提交回复
热议问题