How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?

后端 未结 3 1375
天涯浪人
天涯浪人 2021-02-19 08:21

I have my database with table names starting with \"tbl\" prefix and column names like \"ua_id\" which are understandable in context of project but problematic if used in a mode

相关标签:
3条回答
  • 2021-02-19 08:54

    If you are using Code First, you can simply decorate your model with Table and Column attribute and give it the database names.

    [Table("tbl_Student")]
    public class Student
    {
        [Column("u_id")]
        public int ID { get; set; }
    }
    

    These attributes are available in the System.ComponentModel.DataAnnotations.Schema namespace

    0 讨论(0)
  • 2021-02-19 08:54

    You can keep following inside OnModelCreating

    modelBuilder.Entity<MyModel>()
    .Property(e => e.MyColumn).HasColumnName("DBColumn")
    
    0 讨论(0)
  • 2021-02-19 08:54

    And if you're not using code-first, just select a table in the model diagram, hit F4 (properties) and change the name.

    0 讨论(0)
提交回复
热议问题