Mapping Columns in Entity Framework Code First

后端 未结 2 933
鱼传尺愫
鱼传尺愫 2021-02-01 14:17

I\'m having trouble trying to map my EF 4.1 Code First model to a database. Everything works fine when the database match the code exactly, but when I try and map when the colum

相关标签:
2条回答
  • 2021-02-01 14:51

    How about using DataAnnotations?

    [Key]
    [Column("ColID", TypeName="int")]
    public int DinnerID { get; set; }  
    

    You can find a full list with samples at http://msdn.microsoft.com/en-us/data/gg193958

    0 讨论(0)
  • 2021-02-01 14:58

    I believe you just have to do

    modelBuilder.Entity<Dinner>().Property(x => x.HostedBy).HasColumnName("colHost");
    

    for all of your columns that you want the db column names to be named differently than their property names.


    Edit: After some more searching I came across this question. Judging from that and the error you posted, it seems like the mc.Properties() is more for splitting values into different tables, not to actually rename those table names. I think renaming will still have to be done on a manual basis.

    This is again information from googling and I have no idea if I am just missing a piece to do exactly what you want :).

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