Mapping to already existing database table

南笙酒味 提交于 2019-12-04 15:22:52

You need to instruct EF to ignore IsActive property and map other properties. If you don't like data annotations you can do this with fluent API:

modelBuilder.Entity<Bank>().Ignore(b => b.IsActive);
modelBuilder.Entity<Bank>().Property(b => b.Id).HasColumnName("BankID");
modelBuilder.Entity<Bank>().Property(b => b.Name).HasColumnName("BankName");

Try adding the following attribute to the IsActive property and see if it helps

[NotMapped]

UPDATE

Use Data Annotations instead of fluent API which is easier to grasp and use IMO

First of all remove the OnModelCreating function from your context class

Then Define your model like this

[Table("Bank")]
public class Bank
{
    [key]
    public int Id { get; set; }
    [Column("BankName")]
    public string Name { get; set; }
    [NotMapped]
    public bool IsActive { get; set; }
}

This shall work as expected

You could use System.ComponentModel.DataAnnotations to map the property names to match those in your existing table e.g.

    [Column("BankID")]
    [Required]
    public string Id
        {
        get;
        set;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!